我试图让我的 View Controller 检测我的应用程序自动显示的 UINavigationBar 中的滑动,但它拒绝检测滑动。有什么办法可以做到吗?
最佳答案
假设您想检测导航栏中向左的滑动,您可以在创建导航 Controller 时做这样的事情
: UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.navigationController.navigationBar addGestureRecognizer:swipeLeft];
然后创建一个类似下面的方法来处理它:
-(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {
if (gesture.state != UIGestureRecognizerStateEnded) {
return;
}
//do something
}
OBS:由于导航 Controller 是一个会在应用程序生命周期的几个步骤中保持事件状态的类,因此请务必注意这一点并仅在创建导航 Controller 时添加手势识别器(这意味着仅添加它一次)这样你就不会 Handlebars 势识别器一个接一个地堆在一起,这不仅会导致内存泄漏,而且还可能使你的方法 didSwipedLeft
被调用不止一次。
关于iphone - 在 UINavigationBar 中检测滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1802135/