我正在开发一个开源杂志引擎,你可以在 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
最佳答案
我解决了我自己的问题。
我不得不告诉我的 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;
}
关于ios - UIGestureRecognizers 干扰 UIToolbar?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8379682/