gpt4 book ai didi

objective-c - UIBarButtonItem 和 UIGestureRecognizer

转载 作者:行者123 更新时间:2023-12-04 05:18:16 26 4
gpt4 key购买 nike

我有一个 UIView我在哪里添加了 UITapGestureRecognizer :

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

然后我添加一个 UIToolBar带有 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];

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

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

最佳答案

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

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

不要忘记将手势的代表设置为 self
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *mainTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainTapGesture:)];
mainTapGestureRecognizer.delegate = self;
[self.view addGestureRecognizer:mainTapGestureRecognizer];
}

关于objective-c - UIBarButtonItem 和 UIGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com