UITapGestureRecognizer点击self.view但忽略子视图 [英] UITapGestureRecognizer tap on self.view but ignore subviews

查看:250
本文介绍了UITapGestureRecognizer点击self.view但忽略子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个功能,当我双击self.view(UIViewCotroller的视图)时,将调用一些代码.但是问题是我在此视图上还有其他UI对象,并且不想将任何识别器对象附加到所有这些对象上.我在下面的方法中找到了此方法,该方法可以在我的视图上做手势,而且我知道它是如何工作的.现在,我处于障碍面前,该选择哪种方法来创建忽略子视图的识别器.有任何想法吗?谢谢.

I need to implement a feature that will invoke some code when I double tap on the self.view (view of UIViewCotroller). But the problem that I have other UI object on this view and I don't want to attach any recognizer object to all of them. I found this method below how to make gesture on my view and I know how it works. Right now I am in front of handicap which way to choose for create this recognizer ignoring subview. Any ideas? Thanks.

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:doubleTap];

推荐答案

您应在self对象中采用UIGestureRecognizerDelegate协议,并调用以下方法检查视图.在此方法内,对照touch.view检查视图并返回适当的布尔值(是/否).像这样:

You should adopt the UIGestureRecognizerDelegate protocol inside the self object and call the below method for checking the view. Inside this method, check your view against touch.view and return the appropriate bool (Yes/No). Something like this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isDescendantOfView:yourSubView]) {
        return NO;
    }
    return YES;
}

也请检查@Ian的答案!

Please, also check @Ian's answer!

快捷键5

// MARK: UIGestureRecognizerDelegate methods, You need to set the delegate of the recognizer
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
     if touch.view?.isDescendant(of: tableView) == true {
        return false
     }
     return true
}

这篇关于UITapGestureRecognizer点击self.view但忽略子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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