简体   繁体   中英

Detecting Swipe In UINavigationBar

I am trying to get my view controller to detect swipes in the UINavigationBar 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM