0

在我的 SwiftUI 应用程序中,文本的某些部分需要可点击。在点击时,应该会发生一些自定义操作,而不一定是打开网页。同时我需要检测水龙头坐标。我打算为此使用拖动手势处理程序。

我使用AttributedString. 问题是我无法检测到点击坐标,因为点击链接时不会调用点击或拖动手势的处理程序。

关于如何在 SwiftUI 中同时检测链接上的点击和点击坐标的任何想法?

(我不想为此使用 webview,因为我的应用程序是一个阅读应用程序,它具有很多与文本相关的功能。)

下面是一个代码示例。我为我的应用程序定义了一个自定义 URL 方案,以便能够为链接实现自定义处理程序。

import SwiftUI

struct TappableTextView: View {
    
    var body: some View {
        VStack {
            Text(makeAttributedString()).padding()
                .gesture(
                    DragGesture(minimumDistance: 0)
                        .onEnded({ (value) in
                            print("Text has been tapped at \(value.location)")
                        })
                )
            Spacer()
        }
        .onOpenURL { url in
            print("A link is tapped, url: \(url)")
        }
    }
    
    func makeAttributedString() -> AttributedString {
        var string = AttributedString("")
        
        let s1 = AttributedString("This is a long paragraph. Somewhere in the paragraph is ")

        var tappableText = AttributedString("tappable text")
        tappableText.link = URL(string: "customurlscheme:somedata")
        tappableText.foregroundColor = .green
        
        let s2 = AttributedString(". This is the rest of the paragraph. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
        
        string.append(s1)
        string.append(tappableText)
        string.append(s2)
        
        return string
    }
}
4

1 回答 1

1

你需要使用simultaneousGesture(_:including:). 这是因为您已经在单击链接,因此不会出现正常手势。使用simultaneousGesture意味着您可以同时单击链接并获取坐标。

代码:

Text(makeAttributedString()).padding()
    .simultaneousGesture(
        /* ... */
    )
于 2022-02-03T15:37:57.143 回答