为 iOS [Swift, iOS 15] 编写 PDF 查看器时无法激活三次点击手势

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

我正在尝试创建一个 PDF 查看器,用户可以在其中双击 select 个单词。

我希望它不会与 PDFViewer 的双击手势冲突,以便用户可以双击自动缩放。

但是,我无法使用以下代码激活三次点击。

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

由于某些原因,在指定 tapgesture.numberOfTapsRequired = 2 时可以激活双击。但是,这会干扰双击手势,我无法使用自动缩放功能。

您可以通过实施 shouldBeRequiredToFailBy 委托方法解决此问题:

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

并将委托设置为您的手势识别器:

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