UIBarButtonItem 和 UIGestureRecognizer [英] UIBarButtonItem and UIGestureRecognizer

查看:73
本文介绍了UIBarButtonItem 和 UIGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView,我在其中添加了一个 UITapGestureRecognizer:

I have a UIView where i added a UITapGestureRecognizer:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
tapRecognizer.numberOfTapsRequired=1;
tapRecognizer.numberOfTouchesRequired=1;
[self.myView addGestureRecognizer:tapRecognizer];

然后我将一个带有按钮的 UIToolBar 添加到视图中:

I then add a UIToolBar with a button to the view:

UIToolbar *topBar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width, 44)];
topBar.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *logout = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStyleBordered target:self action:@selector(logout)];

[topBar setItems:@[logout] animated:NO];

我在单击注销按钮时遇到问题,但我的点击识别器触发而不是注销操作.如果我点击并按住,则注销操作将触发(我猜点击识别器失败,所以让按钮操作触发).

I'm having an issue where I click on the logout button, and my tap recognier fires instead of my logout action. If I click and hold, then the logout action will fire (I'm guessing the tap recognizer is failing so lets the buttion action fire).

如何在按下按钮时不触发手势识别器?

how can I not fire the gesture recognizer when the button is pressed?

推荐答案

刚遇到同样的问题.因为我不想引入容器视图(UIToolbar 应该覆盖我现有的视图).在 Patrick.Ji 粗略的指点的帮助下,我想出了这个:

Just had the same problem. Because I don't want to introduce container views (the UIToolbar should cover my existing view). With the help of Patrick.Ji's coarsely pointing I came up with this:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view.superview isKindOfClass:[UIToolbar class]]) {
        return NO;
    }
    return YES;
}

不要忘记将手势的委托设置为self

Don't forget to set the delegate of the gesture to self

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *mainTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainTapGesture:)];
    mainTapGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:mainTapGestureRecognizer];
}

这篇关于UIBarButtonItem 和 UIGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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