IOS:当一个Imageview被触摸时事件。

时间:2023-01-22 20:26:49

I have an ImageView with a png and I want to do this: when someone touch this imageview it's alpha change to 0.0, is it possible? (all without buttons)

我有一个带有png的ImageView我想这样做:当有人触摸这个ImageView时它的alpha值变为0,可能吗?(没有按钮)

5 个解决方案

#1


6  

Yes, it is possible. For example you can do that with following steps:

是的,这是可能的。例如,你可以通过以下步骤做到:

  1. set image view's userInteractionEnabled property to YES - so it will receive touch events
  2. 将图像视图的userInteractionEnabled属性设置为YES——这样它将接收触摸事件
  3. add UITapGestureRecongnizer to it
  4. 添加UITapGestureRecongnizer
  5. in gesture handler set view's alpha to 0.0, you can do that with animation as well:

    在手势处理程序中,将视图的alpha值设为0.0,你也可以用动画来做:

    [UIView animateWithDuration:0.5 animations:^(void){
            imageView.alpha = 0.0f;
        }];
    

#2


15  

you can use UITapGestureRecognizer added to the UIImageView via addGestureRecognizer

你可以使用UITapGestureRecognizer通过addGestureRecognizer添加到UIImageView中

snippets:

片段:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[iv addGestureRecognizer:singleTap];
[iv setUserInteractionEnabled:YES];

and

- (void)imageTaped:(UIGestureRecognizer *)gestureRecognizer {
    [UIView animateWithDuration:0.5 animations:^(void){
         imageView.alpha = 0.0f;
    }];
}

#3


4  

There are already lots of questions like this. Searching with google gave me the following:

已经有很多这样的问题了。通过谷歌搜索,我得到了以下信息:

touches event handler for UIImageView

触摸UIImageView的事件处理程序

UIImageView Touch Event

UIImageView触摸事件

how can i detect the touch event of an UIImageView

如何检测UIImageView的触摸事件

#4


1  

The code in Swift

迅速的代码

In my case I implemented the tap gesture for an image click

在我的例子中,我实现了图像点击的tap手势

1 - Link the image with the ViewController, by drag and drop

1 -通过拖放将图像与视图控制器连接

@IBOutlet weak var imgCapa: UIImageView!

2 - Instance the UITapGestureRecognizer in ViewDidLoad method:

2 -以ViewDidLoad方法中的UITapGestureRecognizer为例:

override func viewDidLoad() {
    super.viewDidLoad()

    //instance the UITapGestureRecognizer and inform the method for the action "imageTapped"
    var tap = UITapGestureRecognizer(target: self, action: "imageTapped")

    //define quantity of taps
    tap.numberOfTapsRequired = 1
    tap.numberOfTouchesRequired = 1

    //set the image to the gesture 
    imgCapa.addGestureRecognizer(tap)
}

3 - Create the method to do what do you want when the image clicked

3 -创建方法,当图像被点击时,做你想做的事情

func imageTapped(){
    //write your specific code for what do you want     

    //in my case I want to show other page by specific segue 
    let sumario = self.storyboard?.instantiateViewControllerWithIdentifier("sumarioViewController") as SumarioViewController

    self.performSegueWithIdentifier("segueSumarioViewController", sender: sumario)
}

#5


0  

In recent Xcode, this is pretty easy. Go into the storyboard, in the object library search for "gesture", drag the one you want onto the image view. You can then treat the gesture object in the view hierarchy as the thing being tapped, i.e. control-drag from there to your view controller to connect the event handler.

在最近的Xcode中,这非常简单。进入故事板,在对象库中搜索“手势”,将你想要的拖到图像视图中。然后,您可以将视图层次结构中的手势对象视为被点击的对象,即control-drag到您的视图控制器来连接事件处理程序。

Once there you can set the alpha as you like, although if you're trying to essentially remove the image view, you should set imageView.hidden = true / imageView.hidden = YES instead, because that will stop it receiving events.

一旦你可以设置alpha,尽管如果你想要删除图像视图,你应该设置imageView。隐藏= true / imageView。隐藏=是的,因为那会阻止它接收事件。

#1


6  

Yes, it is possible. For example you can do that with following steps:

是的,这是可能的。例如,你可以通过以下步骤做到:

  1. set image view's userInteractionEnabled property to YES - so it will receive touch events
  2. 将图像视图的userInteractionEnabled属性设置为YES——这样它将接收触摸事件
  3. add UITapGestureRecongnizer to it
  4. 添加UITapGestureRecongnizer
  5. in gesture handler set view's alpha to 0.0, you can do that with animation as well:

    在手势处理程序中,将视图的alpha值设为0.0,你也可以用动画来做:

    [UIView animateWithDuration:0.5 animations:^(void){
            imageView.alpha = 0.0f;
        }];
    

#2


15  

you can use UITapGestureRecognizer added to the UIImageView via addGestureRecognizer

你可以使用UITapGestureRecognizer通过addGestureRecognizer添加到UIImageView中

snippets:

片段:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTaped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[iv addGestureRecognizer:singleTap];
[iv setUserInteractionEnabled:YES];

and

- (void)imageTaped:(UIGestureRecognizer *)gestureRecognizer {
    [UIView animateWithDuration:0.5 animations:^(void){
         imageView.alpha = 0.0f;
    }];
}

#3


4  

There are already lots of questions like this. Searching with google gave me the following:

已经有很多这样的问题了。通过谷歌搜索,我得到了以下信息:

touches event handler for UIImageView

触摸UIImageView的事件处理程序

UIImageView Touch Event

UIImageView触摸事件

how can i detect the touch event of an UIImageView

如何检测UIImageView的触摸事件

#4


1  

The code in Swift

迅速的代码

In my case I implemented the tap gesture for an image click

在我的例子中,我实现了图像点击的tap手势

1 - Link the image with the ViewController, by drag and drop

1 -通过拖放将图像与视图控制器连接

@IBOutlet weak var imgCapa: UIImageView!

2 - Instance the UITapGestureRecognizer in ViewDidLoad method:

2 -以ViewDidLoad方法中的UITapGestureRecognizer为例:

override func viewDidLoad() {
    super.viewDidLoad()

    //instance the UITapGestureRecognizer and inform the method for the action "imageTapped"
    var tap = UITapGestureRecognizer(target: self, action: "imageTapped")

    //define quantity of taps
    tap.numberOfTapsRequired = 1
    tap.numberOfTouchesRequired = 1

    //set the image to the gesture 
    imgCapa.addGestureRecognizer(tap)
}

3 - Create the method to do what do you want when the image clicked

3 -创建方法,当图像被点击时,做你想做的事情

func imageTapped(){
    //write your specific code for what do you want     

    //in my case I want to show other page by specific segue 
    let sumario = self.storyboard?.instantiateViewControllerWithIdentifier("sumarioViewController") as SumarioViewController

    self.performSegueWithIdentifier("segueSumarioViewController", sender: sumario)
}

#5


0  

In recent Xcode, this is pretty easy. Go into the storyboard, in the object library search for "gesture", drag the one you want onto the image view. You can then treat the gesture object in the view hierarchy as the thing being tapped, i.e. control-drag from there to your view controller to connect the event handler.

在最近的Xcode中,这非常简单。进入故事板,在对象库中搜索“手势”,将你想要的拖到图像视图中。然后,您可以将视图层次结构中的手势对象视为被点击的对象,即control-drag到您的视图控制器来连接事件处理程序。

Once there you can set the alpha as you like, although if you're trying to essentially remove the image view, you should set imageView.hidden = true / imageView.hidden = YES instead, because that will stop it receiving events.

一旦你可以设置alpha,尽管如果你想要删除图像视图,你应该设置imageView。隐藏= true / imageView。隐藏=是的,因为那会阻止它接收事件。