IOS:触摸 Imageview 时的事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7740375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:50:09  来源:igfitidea点击:

IOS: event when an Imageview is touched

iosxcodeuiimageview

提问by cyclingIsBetter

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.0,这可能吗?(全部没有按钮)

回答by Tek Yin

you can use UITapGestureRecognizeradded to the UIImageViewvia addGestureRecognizer

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

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;
    }];
}

回答by Vladimir

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. add UITapGestureRecongnizer to it
  3. in gesture handler set view's alpha to 0.0, you can do that with animation as well:

    [UIView animateWithDuration:0.5 animations:^(void){
            imageView.alpha = 0.0f;
        }];
    
  1. 将图像视图的 userInteractionEnabled 属性设置为 YES - 这样它就会接收触摸事件
  2. 添加 UITapGestureRecongnizer 到它
  3. 在手势处理程序中将视图的 alpha 设置为 0.0,您也可以使用动画来做到这一点:

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

回答by Chris Grant

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 的触摸事件

回答by Weles

The code in Swift

Swift 中的代码

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

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

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

1 - 通过拖放将图像与 ViewController 链接

@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)
}

回答by Jim Driscoll

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 中,这很容易。进入故事板,在对象库中搜索“手势”,将你想要的拖到图像视图上。然后,您可以将视图层次结构中的手势对象视为被点击的对象,即从那里控制拖动到您的视图控制器以连接事件处理程序。

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 = YESinstead, because that will stop it receiving events.

到达那里后,您可以根据需要设置 alpha,但如果您试图从根本上删除图像视图,则应该设置imageView.hidden = true/imageView.hidden = YES代替,因为这将阻止它接收事件。