导航链接被点击后如何执行操作? [英] How to perform an action after NavigationLink is tapped?

查看:19
本文介绍了导航链接被点击后如何执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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

It works fine with single tap. But when I long press NavigationLink it doesn't work. How should I rewrite my code to include long press as well? Or maybe I should make it work different than using simultaneousGesture?

推荐答案

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

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

这篇关于导航链接被点击后如何执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆