【问题标题】:Why UITapGestureRecognizer is not Work For UIImageView?为什么 UITapGestureRecognizer 不适用于 UIImageView?
【发布时间】:2013-06-16 23:40:34
【问题描述】:

我已将 Simple UITapGestureRecognizer 添加到 UIImageView 并创建了 Gesture Method,当用户点击/触摸时会调用它 UIImageView

我的代码是:

UITapGestureRecognizer *eagleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eagleTapGestureIsCall:)];
eagleTapGesture.numberOfTapsRequired = 1;
[self.egaleImgView addGestureRecognizer:eagleTapGesture]; 

手势方法:

- (void)eagleTapGestureIsCall:(UITapGestureRecognizer *)recognizer
{
    CGPoint location = [recognizer locationInView:[recognizer.view superview]];
    NSLog(@"Eagle is Tapped X - %f",location.x);
    NSLog(@"Eagle is Tapped Y - %f",location.y);
}

这里如果我在UIView 而不是self.egaleImgView 上添加手势,那么它可以工作(我的意思是调用手势方法)但是为什么它不能在self.egaleImgView 上工作??? p>

谷歌搜索后发现,可能是UIView在触摸到达UIImageView之前拦截了触摸。

所以我在viewDidLoad的开头添加了以下行

self.view.userInteractionEnabled = NO; /// Here i set userInteractionEnabled = NO;

以下描述结构以便更好地理解。

                main UIView
                  |     |

(Sub view) mainBackGroungImageView //// size is similar to size of main UIView
                  |     |
        (Sub view) gestureAdded ImageView 

已编辑:

抱歉,忘记提及我已将 self.egaleImgView.userInteractionEnabled = YES; 添加到我的 imageView,但它不适合我。

请就我的问题给我建议。

提前致谢。

【问题讨论】:

  • 您是否启用了用户交互?在图像视图本身上。
  • 默认情况下,图像视图禁用用户操作
  • 不要在视图上禁用userInteraction
  • 能否检查一下imageView的superView是否没有裁剪imageView。

标签: iphone ios objective-c uiimageview uigesturerecognizer


【解决方案1】:

设置您的ImageView.userInteractionEnabled = YES;,因为它默认为NO。最好也将视图的用户交互设置为 YES。

【讨论】:

  • 对不起,我忘了提到我已经完成了(我把 self.egaleImgView.userInteractionEnabled = YES; 在我的代码中);但它对我没用:(
  • 我有同样的问题,但这个答案解决了我的问题!
【解决方案2】:

如果您在父视图上设置userInteractionEnable = NO,它将级联到所有子视图。保持启用状态。您可以尝试以下操作,而不是点击手势:

为了获得对图像视图的点击,您可以使用- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 方法。

在您的视图的-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 中,您可以获得被触摸的视图。它可以是这样的,

CGPoint locPoint = [touch locationInView:self.view];
UIView* touchedSubView = [self.view hitTest:locPoint withEvent:event];

然后你可以为视图类添加一个检查并实现所需的内容,就像这样

if ([touchedSubView isKindOfClass:[UIImageView class]]) {
    //do stuff
}

希望对你有帮助

【讨论】:

    【解决方案3】:

    而不是[recognizer locationInView:[recognizer.view superview]];

    使用 [识别器 locationInView:self];

    【讨论】:

    • 这是什么这不是我的问题,请先阅读我的问题,它对我来说是唯一的信息,对我来说并不重要:(
    【解决方案4】:

    UIImageView userInteractionEnabled 已禁用。让它不能。

    UITapGestureRecognizer *eagleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(eagleTapGestureIsCall:)];
    eagleTapGesture.numberOfTapsRequired = 1;
    [self.egaleImgView addGestureRecognizer:eagleTapGesture]; 
    self.egaleImgView.userInteractionEnabled = YES;
    

    我有同样的问题。这样就解决了。

    【讨论】:

    • 那么其他一些观点就在上面了。您是否添加了任何其他视图?
    • 我也尝试做 [self.mainBG bringSubviewToFront:self.view]; [self.egaleImgView带来SubviewToFront:self.mainBG];但它对我不起作用:(
    • 在插入 egaleImgView 时使用 insertsubviewAtIndex:99。
    • 感谢重播,但我听不懂你在说什么>?请添加一些描述作为答案,谢谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2012-07-18
    • 2014-07-29
    相关资源
    最近更新 更多