I am trying to get my view controller to detect swipes in the UINavigatio开发者_开发百科nBar that is automatically displayed by my app, but it refuses to detect swipes. Is there any way I can do it?
Supposing you want to detect swipes to the left in your navigation bar, you could do something like this when you create the navigation controller:
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewSwipedLeft:)];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.navigationController.navigationBar addGestureRecognizer:swipeLeft];
and then create a method like the one below to handle it:
-(void) didSwipedLeft: (UISwipeGestureRecognizer *) gesture {
if (gesture.state != UIGestureRecognizerStateEnded) {
return;
}
//do something
}
OBS: As you navigation controller is a class that will remain alive for several steps of you application life cycle, it is important to pay attention to that and add the gesture recognizer only when you create the navigation controller (which means only add it once) so that you dont keep piling gestures recognizer one over each other, wich will lead not only to a memory leak, but also might make your method didSwipedLeft
to be called more than once.
精彩评论