iOS: right swipe gesture not responding in UIPageViewController

2.2k views Asked by At

I am currently facing a scenario that I am using a UIPageViewController on a view & I want the right swipe gesture to work when user swipe right side from index value 0, so I am trying to add a right swipe gesture to UIPageViewController by the following code:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
//Edit 1
[self.pageViewController.view addGestureRecognizer:rightRecognizer];

but, while I am adding gesture to pageviewcontroller I am getting following error message (Solved by @Lyndsey Scott answer)

no visible interface uipageviewcontroller declares the selector addgesturerecognizer

Can anyone guide me that how can I implement the right swipe in this pageViewController

Edit 1: Replaced the code provided by @Lyndsey Scott, removed the error, but problem still exists that I am not able to trigger that swipe event.

2

There are 2 answers

11
Lyndsey Scott On BEST ANSWER

You have to the gesture to the UIPageViewController's view:

[self.pageViewController.view addGestureRecognizer:rightRecognizer];

Edit in response to your edit:

The swipe gesture won't work if you haven't implemented any UIGestureRecognizerDelegate methods to allow for the UIPageViewController's swipe gesture and your swipe gesture to be recognized simultaneously. Right now, your swipe is essentially being blocked by the page controller's gesture. You could change that by implementing the UIGestureRecognizerDelegate, setting rightRecognizer.delegate = self;, and overriding gestureRecognizer: shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer such that it returns true in the case that both gesture recognizers are triggered.

But if you want the page to flip and your gesture to be recognized at the time of that page flip, adding the extra swipe gesture is unnecessary since you can simply trigger your method upon page turn; for example in pageViewController:didFinishAnimating:previousViewControllers: transitionCompleted:.

1
Alistra On

You shouldn't have to add the gesture recognizer, if you implement the protocol correctly it should work out of the box.

The point of UIPageViewController is to not have to do it manually.