2
votes

I'm having difficulties with UISwipeGestureRecognizer which works on the first page of a UIPageViewController project but not on all the following ones.

The configuration is based on Apple's PageViewController sample code. The UISwipeGestureRecognizer in question is added in the storyboard as well as a UITapGestureRecognizer which works fine on every page.

I checked if the target, selector, view is correct on the view controller but couldn't find anything unusual.

Has somebody noticed a similar behavior and found a solution.

I should say that I tried to add the swipe gesture recognizer programmatically with the same result.

1
Do you want the swipe recognizer to work on the content area of the page view controller?TotoroTotoro
Its actually part of the view controllers view (added by the data source).Bernd Rabe

1 Answers

3
votes

I was having the same issue and found two ways to solve it.

1.This way allows both the Pan and Swipes to be recognized at the same time, which may be what you want. This is not what I wanted as I didn't want the page to change when I swiped up/down. For this method you'll have to make your class the delegate of the swipe gesture recognizers.

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

2.This way prevents the Pan from going until it knows the Swipes have failed, meaning that a swipe will never occur at the same time as a pan. This will only work for you if your swipes are vertical, as horizontal ones would always prevent the pan, I believe.

//Cheat to get the pan gesture from the pageviewcontroller. You should iterate and make sure you get the right one.
UIPanGestureRecognizer * panGR = self.pageViewController.gestureRecognizers[0];     

// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
UISwipeGestureRecognizer * swipeGestureRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(openArchive:)];
swipeGestureRec.direction = UISwipeGestureRecognizerDirectionDown;
[panGR requireGestureRecognizerToFail:swipeGestureRec];
[self.view addGestureRecognizer:swipeGestureRec];
swipeGestureRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(closeArchive:)];
swipeGestureRec.direction = UISwipeGestureRecognizerDirectionUp;
[panGR requireGestureRecognizerToFail:swipeGestureRec];
[self.view addGestureRecognizer:swipeGestureRec];