如何在UIImageview中添加手势识别器? [英] How to add gesture recognizer to a UIImageview?

查看:178
本文介绍了如何在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 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
  • 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.

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

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

这篇关于如何在UIImageview中添加手势识别器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆