在 UIRepresentableView 和 SwiftUI 的视图之间设置独占触摸

Set exclusive touch between UIRepresentableView and SwiftUI's View

我有一个 UITextView 的 UIViewRepresentable 实现,能够在 上显示和处理 links

...
struct TextViewWrapper: UIViewRepresentable {
    ....
     func makeUIView(context: Context) -> UITextView {
        let textView = UITextView()
        textView.text = text
        textView.textColor = foregroundColor
        textView.font = font
        textView.backgroundColor = .white
        textView.dataDetectorTypes = .link
        textView.isEditable = false
        textView.isSelectable = true
        textView.isScrollEnabled = false
        textView.delegate = context.coordinator
        textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        textView.isExclusiveTouch = true
        recalculateHeight(view: textView, result: $calculatedHeight)
        return textView
    }
   ....

}

// Coordinator
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
    onSelectLink(URL)
    return false
}
...

这在 SwiftUI 视图中的使用方式如下:

struct Content: View {
    var body: some View {
        VStack {
            TextViewWrapper(text: "Text with link https://google.com", onSelectLink: { _ in print("On select link") })
        }
        .onTapGesture { print("On tap gesture") }
    }
}

注意:这是一个简化的示例。但问题是,与 link 交互是调用 onSelectLink 的回调,但也允许执行 onTapGesture 识别器。我希望 UITextView 触摸优先,而 onTapGesture 被忽略。

一个可能的解决方案是将onTapGesture添加到TextViewWrapper并忽略它:

VStack {
    TextViewWrapper(text: "Text with link https://google.com", onSelectLink: { _ in print("On select link") })
        .onTapGesture {
            // ignore this tap
        }
}.onTapGesture {
    print("On tap gesture")
}