如何在UIImageview中添加手势识别器?

pqt*_*eru 5 uiview uigesturerecognizer ios

在项目中,UIViewmyView和UIImageViewmyImage背后是myView,视图层次结构:

UIWindow
| - UIImageView(myImage)
| - UIView(myView)[全屏]

然后在ViewController中为myImage添加了一个手势识别器

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        tapGesture.numberOfTapsRequired = 1;
        [myImage addGestureRecognizer:tapGesture];
Run Code Online (Sandbox Code Playgroud)

但是手势不会影响myImage,myImage有setUserInteractionEnabled:YES.它只会影响myImage放在myView前面的时候,我该如何解决这个问题呢?

Ali*_*are 11

UIView明显可以在接触之前拦截触摸UIImageView.

你可以把你放在你UIImageView面前UIView,也可以禁用用户互动,UIView这样它就不会拦截触摸.

如果您需要更精细的纹理来捕捉触摸事件,您可以:

  • 把你UIView的顶部UIImageView,如果有设计目的(如果它掩盖了UIImageView一个位为例),但使它抓不住事件(userInteractionEnabled = NO),并使用不同的UIView下面UIImageView居然赶上之外的事件UIImageView
  • 保持你UIView的顶部UIImageView并保持它捕捉事件(userInteractionEnabled = YES),但过滤通过它的事件,通过继承你的UIView和重写pointInside:withEvent:hitTest:withEvent:方法.

有关更多信息,您应该阅读与事件处理相关的专用Apple编程指南.