点击 NavigationLink 后如何执行操作?

mal*_*low 13 swiftui

我的第一个视图中有一个加号按钮。看起来像一个 FAB 按钮。我想在点击包含在 NavigationLink 中的某个步骤后隐藏它。到目前为止,我有这样的事情:

ForEach(0 ..< 12) {item in
    NavigationLink(destination: TransactionsDetailsView()) {
        VStack {
            HStack(alignment: .top) {
                Text("List item")
            }
            .padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
            .foregroundColor(.black)
            Divider()
        }
    }
    .simultaneousGesture(TapGesture().onEnded{
        self.showPlusButton = false
    })
        .onAppear(){
            self.showPlusButton = true
    }
}
Run Code Online (Sandbox Code Playgroud)

单击即可正常工作。但是当我长按 NavigationLink 时它不起作用。我应该如何重写我的代码以包括长按?或者我应该让它的工作方式与使用 concurrentGesture 不同?

ars*_*ius 14

我正在使用以下代码。我更喜欢它NavigationLink本身,因为它可以让我重用现有的ButtonStyles。


struct NavigationButton<Destination: View, Label: View>: View {
    var action: () -> Void = { }
    var destination: () -> Destination
    var label: () -> Label

    @State private var isActive: Bool = false

    var body: some View {
        Button(action: {
            self.action()
            self.isActive.toggle()
        }) {
            self.label()
              .background(
                ScrollView { // Fixes a bug where the navigation bar may become hidden on the pushed view
                    NavigationLink(destination: LazyDestination { self.destination() },
                                                 isActive: self.$isActive) { EmptyView() }
                }
              )
        }
    }
}

// This view lets us avoid instantiating our Destination before it has been pushed.
struct LazyDestination<Destination: View>: View {
    var destination: () -> Destination
    var body: some View {
        self.destination()
    }
}

Run Code Online (Sandbox Code Playgroud)

并使用它:

var body: some View {
  NavigationButton(
    action: { print("tapped!") },
    destination: { Text("Pushed View") },
    label: { Text("Tap me") }
  )
}
Run Code Online (Sandbox Code Playgroud)

  • 哇!这个解决方案非常优雅,NavigationButton 应该会在 2020 年成为 SwiftUI 的一部分! (2认同)

Asp*_*eri 9

是的,NavigationLink不允许这样的同时手势(可能是设计的,可能是由于问题,无论如何)。

您期望的行为可能会实现如下(当然,如果您需要在列表项中添加一些人字形,则需要手动添加)

import SwiftUI

struct TestSimultaneousGesture: View {
    @State var showPlusButton = false
    @State var currentTag: Int?
    var body: some View {

        NavigationView {
            List {
                ForEach(0 ..< 12) { item in
                    VStack {
                        HStack(alignment: .top) {
                            Text("List item")
                            NavigationLink(destination: Text("Details"), tag: item, selection: self.$currentTag) {
                                EmptyView()
                            }
                        }
                        .padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10))
                        .foregroundColor(.black)
                        Divider()
                    }
                    .simultaneousGesture(TapGesture().onEnded{
                        print("Got Tap")
                        self.currentTag = item
                        self.showPlusButton = false
                    })
                    .simultaneousGesture(LongPressGesture().onEnded{_ in
                        print("Got Long Press")
                        self.currentTag = item
                        self.showPlusButton = false
                    })
                    .onAppear(){
                        self.showPlusButton = true
                    }
                }
            }
        }
    }
}

struct TestSimultaneousGesture_Previews: PreviewProvider {
    static var previews: some View {
        TestSimultaneousGesture()
    }
}
Run Code Online (Sandbox Code Playgroud)