【问题标题】:SwiftUI view's onAppear event call on swipe back gesture actionSwiftUI 视图 onAppear 事件调用回扫手势动作
【发布时间】:2021-06-30 17:44:10
【问题描述】:

您可能知道,SwiftUI 的 .onAppear 在视图出现时被调用。

在我的应用程序中,我实现了从子视图到父视图的向后滑动手势。但是,当执行向后滑动手势时,父母的 onAppear 已经部分可见时会调用它。

所以我的问题是:我应该怎么做才能确保在父视图全部(而不是部分)出现时调用 onAppear

【问题讨论】:

    标签: ios swift swiftui swiftui-navigationview


    【解决方案1】:

    您可以在isActive Binding 中检查 NavigationLink 的状态。这是一个使用 ProxyBinding 的示例,您可以在其中检测到 setter 中的滑动。

    struct ContentView: View {
        @State var isActive: Bool = false
    
        var body: some View {
            NavigationView {
                NavigationLink(
                    destination: Text("Destination"),
                    isActive: Binding<Bool>(
                        get: {
                            isActive
                        }, set: {
                            isActive = $0
                            // Here you can check the status, if it is false it is fully slided back. 
                            print($0)
                        }),
                    label: {
                        Text("Navigate")
                    })
            }
            .onAppear {
                print("appear")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2020-10-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多