Search code examples
iosswiftmacospdfkitmac-catalyst

How to know if user finished selecting text inside PDFView


There is a problem that macCatalyst in not support mouse events as it does AppKit with not ported (maccatalyst) macOS apps. I need to know when user finished selecting some portion of text inside PDF file with PDFKit using mouse or track pad. Maybe somebody has solution or can suggest an idea how to implement somethings like mouseUp or touchedEnded methods?


Solution

  • Implement your own GestureRecocongiser:

    import UIKit.UIGestureRecognizerSubclass
    
    class TouchUpGestureRecogniser: UIGestureRecognizer {
        
        override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
            state = UIGestureRecognizerState(rawValue: 3)!
        }
        
        override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
            state = UIGestureRecognizerState(rawValue: 4)!
        }
        
    }
    

    Then inside your VC, where PDFView is located:

    let customGesture = TouchUpGestureRecogniser(target: self, action: #selector(touchUp(_:)))
    customGesture.delegate = self
    pdfView.addGestureRecognizer(customGesture)
    

    and finally:

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

    NOTE: If you have more than 1 gesture on PDFView please test them, if there will be any issues merge them using UIGestureRecognizerDelegate functions.