iosswiftgesturepdfview

Triple tap gesture cannot be activated when programming PDF Viewer for iOS [Swift, iOS 15]


I am trying to create a PDF Viewer, where users can triple-tap to select a word.

I would like to have it without conflicting with a double-tap gesture of the PDFViewer so that users can double-tap to autozoom.

However, I cannot activate the triple-tap using the following code.

let tapgesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(_:)))
tapgesture.numberOfTapsRequired = 3
pdfView.addGestureRecognizer(tapgesture)

For some reason, double-tap can be activated when specifying tapgesture.numberOfTapsRequired = 2. However, this disturbs the double-tap gesture and I cannot use the autozoom function.


Solution

  • You can solve this by implementing shouldBeRequiredToFailBy delegate method:

    extension ViewController: UIGestureRecognizerDelegate {
        func gestureRecognizer(
            _ gestureRecognizer: UIGestureRecognizer,
            shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer
        ) -> Bool {
            if let tapGestureRecognizer = otherGestureRecognizer as? UITapGestureRecognizer,
               tapGestureRecognizer.numberOfTapsRequired == 2
            {
                return true
            }
            return false
        }
    }
    

    And set the delegate to your gesture recognizer:

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
    tapGesture.numberOfTapsRequired = 3
    tapGesture.delegate = self