How to perform an action after NavigationLink is tapped?

后端 未结 4 926
梦毁少年i
梦毁少年i 2020-12-10 14:29

I have a Plus button in my first view. Looks like a FAB button. I want to hide it after I tap some step wrapped in NavigationLink. So far I have something like this:

4条回答
  •  萌比男神i
    2020-12-10 14:49

    Yes, NavigationLink does not allow such simultaneous gestures (might be as designed, might be due to issue, whatever).

    The behavior that you expect might be implemented as follows (of course if you need some chevron in the list item, you will need to add it manually)

    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()
        }
    }
    

提交回复
热议问题