简体   繁体   English

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

[英]How to add gesture recognizer to a UIImageview?

In the project, there are an UIView myView and an UIImageView myImage behinds the myView, views hierarchy: 在项目中,有一个UIView myView和一个UIImageView myImage,它位于myView,views层次结构的后面:

UIWindow 的UIWindow
|-- UIImageView (myImage) | - UIImageView(myImage)
|-- UIView (myView) [whole screen] | - 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 . 但是手势不会影响myImage,myImage有setUserInteractionEnabled:YES It will only affect when myImage placed in front of myView, how can I solve this problem? 它只会影响myImage放在myView前面的时候,我该如何解决这个问题呢?

Your UIView obviously intercept the touches before they can reach the UIImageView . 你的UIView显然可以在它们到达UIImageView之前拦截触摸。

You can either put your UIImageView in front of your UIView , or disable user interaction on the UIView so it does not intercept the touches. 您可以将UIImageView放在UIView前面,也可以禁用UIView上的用户交互,这样它就不会拦截触摸。

If you need finer grain over catching your touch events, you may either: 如果您需要更精细的纹理来捕捉触摸事件,您可以:

  • put your UIView on top of the UIImageView if it has to for design purposes (if it masks the UIImageView a bit for example) but make it not catch events ( userInteractionEnabled = NO ), and use a different UIView below the UIImageView to actually catch the events outside the UIImageView 如果它必须用于设计目的(例如,如果它稍微掩盖了UIImageView ),那么将你的UIView置于UIImageView之上,但是不要捕获事件( userInteractionEnabled = NO ),并使用UIImageView下面的不同UIView实际捕获UIImageView之外的事件
  • keep your UIView on top of the UIImageView and keep it catching events ( userInteractionEnabled = YES ), but filter the events that pass thru it, by subclassing your UIView and overriding either the pointInside:withEvent: or the hitTest:withEvent: method. 保持你的UIViewUIImageView之上,并保持捕获事件( userInteractionEnabled = YES ),但过滤通过它的事件,通过继承你的UIView并覆盖pointInside:withEvent:hitTest:withEvent:方法。

For more information you should really read the dedicated Apple Programming Guide related to Event Handling . 有关更多信息, 您应该阅读与事件处理相关的专用Apple编程指南

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. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM