1
votes

I have a UIViewController that contains a UIPageViewController that uses the scroll style. Users can swipe through the pages as usual. Now I want to add a two finger UISwipeGestureRecognizer to the outer view controller that contains the page view controller. I want users to be able to one finger swipe to switch pages and two finger swipe to trigger another action. But by default, when you swipe with two fingers, iOS scrolls the pages and ignores the two finger gesture. To fix that, I set the pageViewController.scrollView.panGestureRecognizer to require the two finger swipe gesture to fail. Then it will recognize both as desired, but the problem is it makes it difficult to one finger swipe through the pages quickly because it's waiting for the two finger swipe to fail. It ignores a lot of your swipe input so you really have to touch down and hold for a moment before you one finger swipe to switch pages. I've also tried setting the pan gesture's minimumNumberOfTouches and maximumNumberOfTouches to 1 but it behaves the same.

What could I do to make this work well, to only recognize the two finger swipe when swiping with two fingers but not delay recognizing the one finger swipe to switch pages?

1

1 Answers

0
votes

First of all, you need to make sure the UISwipeGestureRecognizer recognize simultaneously with the panGestureRecognizer using UIGestureRecognizerDelegate

func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWith: UIGestureRecognizer) {
   return true
}

Then, when the action method of your two finger UISwipeGestureRecognizer is called, reset the panGestureRecognizer manually.

pageViewController.scrollView.panGestureRecognizer.isEnabled = false
pageViewController.scrollView.panGestureRecognizer.isEnabled = true