Adding a Swipe Gesture Recognizer

You can add a UISwipeGestureRecognizer to PDFViewController’s view at any time:

Copy
1
2
3
4
5
let swipeGestureRecognizer = UISwipeGestureRecognizer()
swipeGestureRecognizer.direction = .left

let pdfViewController = PDFViewController()
pdfViewController.view.addGestureRecognizer(swipeGestureRecognizer)
Copy
1
2
3
4
5
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] init];
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

PSPDFViewController *pdfViewController = [[PSPDFViewController alloc] init]
[pdfViewController.view addGestureRecognizer:swipeGestureRecognizer];

This, however, is not enough to make it work. By default, UIKit will prioritize UIScrollView’s panGestureRecognizer over your UISwipeGestureRecognizer. To reverse that and allow scrolling to proceed only if your swipe gesture recognizer has failed, you’ll need to set up a failure requirement in both documentViewController(_:configureScrollView:) and documentViewController(_:configureZoom:forSpreatAt:) delegate method:

Copy
1
2
3
4
5
6
7
func documentViewController(_ documentViewController: PDFDocumentViewController, configureScrollView scrollView: UIScrollView) {
    scrollView.panGestureRecognizer.require(toFail: self.swipeGestureRecognizer)
}

func documentViewController(_ documentViewController: PDFDocumentViewController, configureZoom zoomView: UIScrollView, forSpreadAt spreadIndex: Int) {
    zoomView.panGestureRecognizer.require(toFail: self.swipeGestureRecognizer)
}
Copy
1
2
3
4
5
6
7
- (void)documentViewController:(PSPDFDocumentViewController *)documentViewController configureScrollView:(UIScrollView *)scrollView {
    [scrollView.panGestureRecognizer requireGestureRecognizerToFail:self.swipeGestureRecognizer];
}

- (void)documentViewController:(PSPDFDocumentViewController *)documentViewController configureZoomView:(UIScrollView *)zoomView forSpreadAtIndex:(NSInteger)spreadIndex {
    [zoomView.panGestureRecognizer requireGestureRecognizerToFail:self.swipeGestureRecognizer];
}

Don’t forget to take experience of scrolling and zooming into account. If you’re adding a swipe gesture recognizer, consider either disabling scrolling and zooming entirely, or conditionally preventing your swipe gesture recognizer from proceeding. For example, to only allow swiping when zoomed out and still allow panning page around when zoomed in, you could write:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func documentViewController(_ documentViewController: PDFDocumentViewController, configureZoom zoomView: UIScrollView, forSpreadAt spreadIndex: Int) {
    zoomView.delegate = self
    zoomView.panGestureRecognizer.require(toFail: self.swipeGestureRecognizer)
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    self.lastKnownZoomScale = scrollView.zoomScale
}

func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
    if gestureRecognizer === self.swipeGestureRecognizer {
        return self.lastKnownZoomScale == 1
    } else {
        return true
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)documentViewController:(PSPDFDocumentViewController *)documentViewController configureZoomView:(UIScrollView *)zoomView forSpreadAtIndex:(NSInteger)spreadIndex {
    zoomView.delegate = self;
    [zoomView.panGestureRecognizer requireGestureRecognizerToFail:self.swipeGestureRecognizer];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    self.lastKnownZoomScale = scrollView.zoomScale;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if gestureRecognizer == self.swipeGestureRecognizer {
        return self.lastKnownZoomScale == 1;
    } else {
        return YES;
    }
}

For more details on how to coordinate multiple gesture recognizers, check out our guide on the document view hierarchy and Apple’s guide on coordinating multiple gesture recognizers.