我正在尝试创建一个PDF查看器,用户可以在其中三次点击选择一个单词。
我希望它不会与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