Skip to content

Commit

Permalink
解决UITextView linkTap 和 单击手势冲突的问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
Finb committed Dec 31, 2024
1 parent 64a66b1 commit ca6dcd2
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion View/MessageList/CustomTapTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit ca6dcd2

Please sign in to comment.