Search code examples
swiftuigesturerecognizerisopdfview

Screen edge gesture is not recognized in PDFView (UIViewer) [Swift, iOS 15, PDFKit]


I am displaying a PDF file and would like to add a screen edge gesture to move pages around.

The following code works fine when entire the content of a PDF is displayed on the screen. However, when the PDF was zoomed, the screen edge gesture cannot even activate.

override func viewDidLoad(){
        super.viewDidLoad()


    //set up gesture to swipe from the edge
    let leftScreenEdgeRecognizer = UIScreenEdgePanGestureRecognizer (
        target: self, action: #selector(TextDocumentViewController.leftEdgePanGestureHandler(_ : )))
    leftScreenEdgeRecognizer.edges = UIRectEdge.left
    
    let rightScreenEdgeRecognizer = UIScreenEdgePanGestureRecognizer (
        target: self, action: #selector(TextDocumentViewController.rightEdgePanGestureHandler(_ : )))
    rightScreenEdgeRecognizer.edges = UIRectEdge.right
    
    //register the gesture
    pdfView.addGestureRecognizer(leftScreenEdgeRecognizer)
    pdfView.addGestureRecognizer(rightScreenEdgeRecognizer)
}



//gesture functions here
@objc func leftEdgePanGestureHandler(_ sender: UIScreenEdgePanGestureRecognizer){     
        if(sender.state == UIGestureRecognizer.State.ended){
        print ("Left Edge")
        pdfView.goToPreviousPage(sender)
    }
    
}

@objc func rightEdgePanGestureHandler(_ sender: UIScreenEdgePanGestureRecognizer){
        if(sender.state == UIGestureRecognizer.State.ended){
        print ("right Edge")
        pdfView.goToNextPage(sender)
    }
}

I tired to add a code like,

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

However, this is not working at all.


Solution

  • I was able to solve this problem thanks to the answer provided for my old question. I have totally forgotten about the post. After learning more about multiple gesture detections through try and error, I realized that I can do as follows to solve this posted question:

    Enables the multiple gesture activation:

    class ViewController: UIViewController, UIGestureRecognizerDelegate,  UIDocumentPickerDelegate, PDFViewDelegate {
    // ... other things
        func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, 
        shouldRecognizeSimultaneouslyWith otherGestureRecognizer: 
        UIGestureRecognizer) -> Bool {
                return true
            }
    }
    

    Make sure to appropriately set delegate.

    override func viewDidLoad(){
            super.viewDidLoad()
    
            //set gesture
            leftScreenEdgeRecognizer.delegate = self
            rightScreenEdgeRecognizer.delegate = self
    }