简体   繁体   English

SwiftUI TapGesture 无法与 iOS 列表中的 NavigationLink 一起使用

[英]SwiftUI TapGesture Not Working with NavigationLink in List on iOS

When I use .simultaneousGesture(TapGesture().onEnded{ //code to run }) on a NavigationLink in SwiftUI for iOS, the code runs fine and the link opens.当我在 iOS 的 SwiftUI 的NavigationLink上使用.simultaneousGesture(TapGesture().onEnded{ //code to run })时,代码运行正常并且链接打开。

However, if the NavigationLink is in a List , the code runs but the link does not open.但是,如果NavigationLinkList中,则代码会运行但链接不会打开。

Is there another way I can run some code after the List NavigationLink is tapped, but before the TextEditor appears?在点击List NavigationLink之后,但TextEditor出现之前,是否有另一种方法可以运行一些代码?

Using .onAppear on the TextEditor , is not suitable because I need the code to run before the TextEditor appears - partly due to a bug with TextEditor 's re-rendering and partly to keep things flowing in the right order.TextEditor上使用.onAppear是不合适的,因为我需要在 TextEditor 出现之前运行代码 - 部分原因是TextEditor的重新渲染存在错误,部分原因是为了让事情以正确的顺序流动。

Code to replicate the issue:复制问题的代码:

import SwiftUI

struct ContentView: View {
    @State private var textString = ""
    @State private var listMembers = ["Link A", "Link B", "Link C"]
    var body: some View {
        NavigationView {
            VStack {
                List(listMembers, id: \.self) { member in
                    NavigationLink(member, destination: TextEditor(text: $textString))
                        .simultaneousGesture(TapGesture().onEnded {
                            textString = "Link A, B, or C was tapped."
                            print(textString)
                    })
                }
                NavigationLink("Link D", destination: TextEditor(text: $textString))
                    .simultaneousGesture(TapGesture().onEnded {
                        textString = "Link D was tapped."
                    })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Thank You!谢谢你!

The best I could come up with is using ForEach instead of List.我能想到的最好的方法是使用 ForEach 而不是 List。 It requires more formatting to make it look nice, but it seems to solve the problem.它需要更多格式才能使其看起来不错,但似乎可以解决问题。

import SwiftUI

struct ContentView: View {
    @State private var textString = ""
    @State private var listMembers = ["Link A", "Link B", "Link C"]
    var body: some View {
        NavigationView {
            VStack {
                /*List(listMembers, id: \.self) { member in
                    NavigationLink(member, destination: TextEditor(text: $textString))
                        .simultaneousGesture(TapGesture().onEnded {
                            textString = "Link A, B, or C was tapped."
                            print(textString)
                    })
                }*/
                ForEach(listMembers, id: \.self) { member in
                    NavigationLink(member, destination: TextEditor(text: $textString))
                        .simultaneousGesture(TapGesture().onEnded {
                            textString = "Link A, B, or C was tapped."
                            print(textString)
                    })
                }
                NavigationLink("Link D", destination: TextEditor(text: $textString))
                    .simultaneousGesture(TapGesture().onEnded {
                        textString = "Link D was tapped."
                    })
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM