UIGestureRecognizers 干扰 UIToolbar?
Posted
技术标签:
【中文标题】UIGestureRecognizers 干扰 UIToolbar?【英文标题】:UIGestureRecognizers interfering with UIToolbar? 【发布时间】:2011-12-05 00:14:00 【问题描述】:我正在开发一个开源杂志引擎,您可以在 GitHub 上查看它:
https://github.com/interactivenyc/Defrag
我已经在一个名为 MenuPanel 的 UIView 中设置了一个 UIToolbar。由于某种原因,UIToolbar 中的 UIBarButtonItems 没有正确调用它们的操作。这是我用于按钮的语法:
UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonClicked:)];
发生的 是,我在屏幕上单击的任何位置,都会调用在我的主 UIViewController 中声明的 UITapGestureRecognizer。这可以在我的主 UIViewController 的这段代码中进行设置:
- (void)setupGestureRecognizers
//NSLog(@"setupGestureRecognizer NEW");
UISwipeGestureRecognizer *swipeRecognizer;
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];
UITapGestureRecognizer *tapRecognizer;
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.view addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
我确信我在尝试执行此操作时在概念上存在一些非常基本的错误。谁能告诉我如何解决这个问题?
作为参考,你可以在这里查看我的主要 DefragViewController:UIViewController:
https://gist.github.com/1431722
还有我的 MenuPanel:这里的 UIView:
gist.github.com/1431728
【问题讨论】:
【参考方案1】:我解决了我自己的问题。
我不得不告诉我的 UIViewController 忽略来自任何 UIToolbar 的触摸,如下所示:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
//ignore any touches from a UIToolbar
if ([touch.view.superview isKindOfClass:[UIToolbar class]])
return NO;
return YES;
【讨论】:
欢迎来到 Stack Overflow!请务必将您的答案标记为已接受,以便其他人知道有解决方案。以上是关于UIGestureRecognizers 干扰 UIToolbar?的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 UIGestureRecognizers 相对于触摸位置在 UIScrollView 内旋转 UIImageView
您可以在不禁用其附加的 UIGestureRecognizers 的情况下禁用 UIButton 吗?
UIGestureRecognizers vs touchesBegan/touchesMoved/touchesEnded(准确度)