2

只是想让我的 navigationController 的 navigationBar 可点击。它适用于

UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
                                    initWithTarget:self action:@selector(toggleMenu)];
tapRecon.delegate = self;
tapRecon.numberOfTapsRequired = 1;
[self.navigationBar addGestureRecognizer:tapRecon];

但是当我有一个后退按钮时,无法点击它(手势可能会接管按钮)。

所以,我尝试了一些在这里找到的东西:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return (![[[touch view] class] isSubclassOfClass:[UIButton class]]);
}

什么都没有,因为[touch view]总是 UINavigationBar ...

我尝试做的最后一件事是cancelsTouchesInViewNO. 没关系,我们可以点击返回按钮,但是还是调用了的toggleMenu动作。UITapGestureRecognizer

您是否有让后退按钮再次起作用但不同时调用的想法toggleMenu

谢谢 !

编辑 :

Juste找到了怎么做:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    for (UIView* sub in self.navigationBar.subviews) {
        NSString *cl = NSStringFromClass([sub class]);
        if ([cl isEqualToString:@"UINavigationItemButtonView"]) {
            CGRect bback = sub.frame;
            CGPoint pointInView = [touch locationInView:gestureRecognizer.view];
            return !CGRectContainsPoint(bback, pointInView);
        }
    }
    return YES;
}
4

2 回答 2

0

好吧,您可以制作一个围绕导航栏的按钮,并制作另一个用于按下的按钮。一定要把它安排在 IB 导航栏按钮的顶部

于 2013-08-20T01:00:19.230 回答
-2

这是 Swift 代码的解决方案:

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    for view in self.navigationBar.subviews {
        if let button = view as? UIButton {
            if touch.view.isEqual(button) {
                return false
            }
        }
    }
    return true
}

只需将其放在您的代表中即可UIGestureRecognizer

于 2015-02-13T16:59:30.333 回答