ios 如何将手势识别器添加到 UIImageview?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12338964/
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 20:02:29  来源:igfitidea点击:

How to add gesture recognizer to a UIImageview?

iosuiviewuigesturerecognizer

提问by pqteru

In the project, there are an UIViewmyView and an UIImageViewmyImage behinds the myView, views hierarchy:

在项目中,在UIViewmyViewUIImageView后面有一个myView和一个myImage,views层次结构:

UIWindow
|-- UIImageView (myImage)
|-- UIView (myView) [whole screen]

UIWindow
|-- UIImageView (myImage)
|-- UIView (myView) 【全屏】

Then added a gesture recognizer to myImage in ViewController

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

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

But gesture doesn't affect myImage, myImage has setUserInteractionEnabled:YES. It will only affect when myImage placed in front of myView, how can I solve this problem?

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

回答by AliSoftware

Your UIViewobviously intercept the touches before they can reach the UIImageView.

UIView显然在他们到达之前拦截了触摸UIImageView

You can either put your UIImageViewin front of your UIView, or disable user interaction on the UIViewso it does not intercept the touches.

您可以将您的UIImageView放在 前面UIView,也可以禁用 上的用户交互,UIView这样它就不会拦截触摸。

If you need finer grain over catching your touch events, you may either:

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

  • put your UIViewon top of the UIImageViewif it has to for design purposes (if it masks the UIImageViewa bit for example) but make it not catch events (userInteractionEnabled = NO), and use a different UIViewbelow the UIImageViewto actually catch the events outside the UIImageView
  • keep your UIViewon top of the UIImageViewand keep it catching events (userInteractionEnabled = YES), but filter the events that pass thru it, by subclassing your UIViewand overriding either the pointInside:withEvent:or the hitTest:withEvent:method.
  • 把你UIView的顶部UIImageView是否具有为设计目的(如果它掩盖了UIImageView一个位为例),但使它抓不住事件(userInteractionEnabled = NO),并使用不同的UIView下面UIImageView居然赶上之外的事件UIImageView
  • 保持你UIView的顶部UIImageView并保持它捕捉事件 ( userInteractionEnabled = YES),但过滤通过它传递的事件,通过子类化你UIView并覆盖 thepointInside:withEvent:或 thehitTest:withEvent:方法。

For more information you should really read the dedicated Apple Programming Guide related to Event Handling.

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

回答by Stefan Brocks

my code:

我的代码:

UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(hasLongPressed)];
[press setMinimumPressDuration:0.1f]; //1ms
press.delegate = (id)self;
[self.DGEMLogo addGestureRecognizer:press];

two properties are neccessary to do

需要做两个属性

1.) add the GestureRecognizer to your UIImageView (self.DGEMLogo) [self.DGEMLogo addGestureRecognizer:press];

1.) 将 GestureRecognizer 添加到您的 UIImageView (self.DGEMLogo) [self.DGEMLogo addGestureRecognizer:press];

2.) set your UIImage property Interaction enabled to TRUE

2.) 将您的 UIImage 属性交互启用设置为 TRUE

thats all.

就这样。