我有一个文本视图--看上去如下:
class StudyText: UITextView, UITextViewDelegate {
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
print(URL)
return false
}
override var canBecomeFirstResponder: Bool {
return false
}
}
这就是结构:
struct ClickableText: UIViewRepresentable {
@Binding var text: NSMutableAttributedString
func makeUIView(context: Context) -> StudyText {
let view = StudyText()
view.dataDetectorTypes = .all
view.isEditable = false
view.isSelectable = true
view.delegate = view
view.isUserInteractionEnabled = true
return view
}
func updateUIView(_ uiView: StudyText, context: Context) {
uiView.attributedText = text
}
}
我正在使用属性链接。
我尝试过的每一种解决方案都不能让链接对快速点击做出响应。马上就去。在打印语句出现之前,这需要一些延迟。
我试过这个:
view.delaysContentTouches = false
我试过这个:
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tappedTextView(tapGesture:)))
self.addGestureRecognizer(tapRecognizer)
@objc func tappedTextView(tapGesture: UIGestureRecognizer) {
let textView = tapGesture.view as! UITextView
let tapLocation = tapGesture.location(in: textView)
let textPosition = textView.closestPosition(to: tapLocation)
let attr = textView.textStyling(at: textPosition!, in: .forward)!
if let url: URL = attr[NSAttributedString.Key(rawValue: NSAttributedString.Key.link.rawValue)] as? URL {
print("clicking here: \(url)")
}
}
但他们都没用。它总是有延迟的回应,我该怎么解决呢?
发布于 2020-06-08 16:24:06
UITextView
既响应单个点击手势(允许您跟踪链接),也响应双点击手势(允许您选择文本)。在你点击一次链接后,不清楚你是否完成了你的手势,或者是否有第二个点击即将到来。只有在没有第二次点击的短时间延迟之后,才能确定您实际上是在执行一个调用textView(_:shouldInteractWith:in:interaction:)
的单点抽头。
不幸的是,在不允许文本选择的情况下,没有标准的方法可以使UITextView
允许以下链接。您可能可以通过在视图上注册的手势识别器搜索,并找到负责识别双击并禁用它的人,但这样做可能会产生意外的副作用。
https://stackoverflow.com/questions/62222043
复制相似问题