如何在UIImageview中添加手势识别器? [英] How to add gesture recognizer to a UIImageview?
问题描述
在项目中,有一个 UIView
myView和一个 UIImageView
myImage支持myView,视图层次结构:
In the project, there are an UIView
myView and an UIImageView
myImage behinds the myView, views hierarchy:
UIWindow
| - UIImageView(myImage)
| - UIView(myView)[全屏]
UIWindow
|-- UIImageView (myImage)
|-- UIView (myView) [whole screen]
然后在ViewController中为myImage添加了手势识别器
Then added a gesture recognizer to myImage in ViewController
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
tapGesture.numberOfTapsRequired = 1;
[myImage addGestureRecognizer:tapGesture];
但是手势不影响myImage,myImage有 setUserInteractionEnabled:YES
。它只会影响myImage放在myView前面的时候,我该如何解决这个问题?
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?
推荐答案
你的 UIView
显然可以在触及 UIImageView
之前拦截触摸。
Your UIView
obviously intercept the touches before they can reach the UIImageView
.
你可以要么将 UIImageView
放在 UIView
前面,要么禁用 UIView <上的用户互动/ code>因此它不会拦截触摸。
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.
如果您需要更精细的抓住触摸事件,您可以:
If you need finer grain over catching your touch events, you may either:
- 将
UIView
放在UIImageView
之上如果它必须用于设计目的(例如,如果它掩盖了UIImageView
),但使其不捕获事件(userInteractionEnabled = NO
),并在UIImageView
下使用不同的UIView
来实际捕获<$ c之外的事件$ c> UIImageView - 保持你的
UIView
在t之上他UIImageView
并保持捕获事件(userInteractionEnabled = YES
),但通过子类化过滤通过它的事件你的UIView
并覆盖pointInside:withEvent:
或hitTest:withEvent:
方法。
- 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.
有关详细信息,您应该阅读与事件处理相关的专用Apple编程指南 。
For more information you should really read the dedicated Apple Programming Guide related to Event Handling.
这篇关于如何在UIImageview中添加手势识别器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!