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
How to add gesture recognizer to a UIImageview?
提问by pqteru
In the project, there are an UIView
myView and an UIImageView
myImage behinds the myView, views hierarchy:
在项目中,在UIView
myViewUIImageView
后面有一个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 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 theUIImageView
if it has to for design purposes (if it masks theUIImageView
a bit for example) but make it not catch events (userInteractionEnabled = NO
), and use a differentUIView
below theUIImageView
to actually catch the events outside theUIImageView
- keep your
UIView
on top of theUIImageView
and keep it catching events (userInteractionEnabled = YES
), but filter the events that pass thru it, by subclassing yourUIView
and overriding either thepointInside:withEvent:
or thehitTest: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.
就这样。