16

In iOS7, we have a new "swipe left to right" gesture, this is a very useful feature, but now I have a problem with it.

I use this code to custom the back button in UINavigationBar.

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];

but this will disables the "swipe left to right" gesture, so I use the follow code to keep the gesture enabled.

self.navigationController.interactivePopGestureRecognizer.delegate = self

And then, I found that the gesture is working abnormal, if current ViewController has a tableView, when the tableView is scrolling, the gesture won't be triggered. Now the gesture can't be triggered easily, is there some way to fix it?

Wei
  • 3,492
  • 4
  • 18
  • 18
  • Have you tried to implement: ´– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:´ on your ´UIGestureRecognizerDelegate´ then you can check if one of the gestures is the swipetopop gesture and return YES. – Peter Segerblom Jan 17 '14 at 09:38
  • @PeterSegerblom It works! And I implemented - `(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer`, `return YES;` to disable the otherGestureRecognizer, so the current ViewController won't receive the `UIPanGestureRecognizer` for scrolling `UITableView`. Now it looks well, thank you very much! – Wei Feb 19 '14 at 09:58
  • Swift 3 solution: http://stackoverflow.com/a/43433530/308315 – iwasrobbed Apr 16 '17 at 03:59

3 Answers3

29

This code can wroks well for me.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return [gestureRecognizer isKindOfClass:UIScreenEdgePanGestureRecognizer.class];
}
Wei
  • 3,492
  • 4
  • 18
  • 18
5

Setting the interactivePopGestureRecognizer.delegate to self is only the first step. The second step is to implement

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return !(otherGestureRecognizer is UIPanGestureRecognizer)
}

The test against the pan gesture recognizer is needed to block vertical scrolling of a scroll or table view while the interactive pop is in progress.

denis_lor
  • 6,212
  • 4
  • 31
  • 55
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
  • 1
    This code won't trigger `UIScreenEdgePanGestureRecognizer` when `UITableView` is scrolling, I implement `- (BOOL)shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer`, and `return YES;`. – Wei Apr 28 '14 at 10:02
5

Add in Class Controller "UIGestureRecognizerDelegate"

in viewDidAppear add

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    
    self.navigationController?.interactivePopGestureRecognizer.delegate =  self
}

=)

ThomasW
  • 16,981
  • 4
  • 79
  • 106
Bruno
  • 1,592
  • 13
  • 14