解决UITextView linkTap 和 单击手势冲突的问题。

This commit is contained in:
Fin 2024-12-31 09:20:43 +08:00
parent 64a66b16b8
commit ca6dcd2be3

View File

@ -15,6 +15,8 @@ class CustomTapTextView: UITextView, UIGestureRecognizerDelegate {
private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
/// tapGesture
private let doubleTapGesture = UITapGestureRecognizer()
/// UITextView
private var linkTapGesture: UIGestureRecognizer? = nil
///
var customTapAction: (() -> Void)?
@ -30,9 +32,10 @@ class CustomTapTextView: UITextView, UIGestureRecognizerDelegate {
self.textContainer.lineFragmentPadding = 0
tapGesture.delegate = self
tapGesture.require(toFail: doubleTapGesture)
self.addGestureRecognizer(tapGesture)
self.linkTapGesture = self.gestureRecognizers?.first { $0 is UITapGestureRecognizer && $0.name == "UITextInteractionNameLinkTap" }
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delegate = self
self.addGestureRecognizer(doubleTapGesture)
@ -62,4 +65,16 @@ class CustomTapTextView: UITextView, UIGestureRecognizerDelegate {
}
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == tapGesture {
if otherGestureRecognizer == doubleTapGesture {
return true
}
if otherGestureRecognizer == linkTapGesture {
return true
}
}
return false
}
}