SwitUI-列表中有两个NavigationLink [英] SwitUI - Two navigationLink in a list

查看:58
本文介绍了SwitUI-列表中有两个NavigationLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的列表单元格中有两个NavigationLink

I have two NavigationLink in a cell of my List

我想在单击一次时转到目标1,而在我单击两次时转到目标2.因此,我添加了两个轻击手势来控制导航.

I want to go to destination1 when I tap once,and go to destination2 when I tap twice. So I added two tap gesture to control the navigation.

但是当我点击时,有两个问题:

But when I tap,there are two questions:

  • 1轻击手势块不会被调用.
  • 2即使两个导航链接都位于TextView的后面,它们也会被自动激活.真正的效果是:点击单元格->转到Destination1->回到家->转到Destination2->回到家

这是我的代码:

struct MultiNavLink: View {
    @State var mb_isActive1 = false;
    @State var mb_isActive2 = false;
    
    var body: some View {
        return
            NavigationView {
                List {
                    ZStack {
                        NavigationLink("", destination: Text("Destination1"), isActive: $mb_isActive1)
                        NavigationLink("", destination: Text("Destination2"), isActive: $mb_isActive2)
                        Text("Single tap::go to destination1\nDouble tap,go to destination2")
                    }
                    .onTapGesture(count: 2, perform: {()->Void in
                        NSLog("Double tap::to destination2")
                        self.mb_isActive2 = true
                    }).onTapGesture(count: 1, perform: {()->Void in
                        NSLog("Single tap::to destination1")
                        self.mb_isActive1 = true
                    })
                }.navigationBarTitle("MultiNavLink",displayMode: .inline)
        }
    }
}

我尝试删除List元素,然后一切按预期进行.似乎是List元素使一切变得奇怪.

I have tried remove the List element,then everything goes as I expected. It seems to be the List element that makes everything strange.

我发现了以下问题: SwiftUI-列表中的两个按钮,但是情况与我不同.

I found this question:SwiftUI - Two buttons in a List,but the situation is different from me.

期待您的回答,非常感谢...

I am expecting for your answer,thank you very much...

推荐答案

尝试以下方法-想法是在可见内容的背景中隐藏链接,并使它们对于UI无效,但通过编程方式激活.

Try the following approach - the idea is to hide links in background of visible content and make them inactive for UI, but activated programmatically.

在Xcode 12/iOS 14上进行了测试.

Tested with Xcode 12 / iOS 14.

struct MultiNavLink: View {

    var body: some View {
        return
            NavigationView {
                List {
                    OneRowView()
                }.navigationBarTitle("MultiNavLink", displayMode: .inline)
        }
    }
}

struct OneRowView: View {
    @State var mb_isActive1 = false
    @State var mb_isActive2 = false

    var body: some View {
        ZStack {
            Text("Single tap::go to destination1\nDouble tap,go to destination2")
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .contentShape(Rectangle())
        .background(Group {
            NavigationLink(destination: Text("Destination1"), isActive: $mb_isActive1) {
                    EmptyView() }
                .buttonStyle(PlainButtonStyle())
            NavigationLink(destination: Text("Destination2"), isActive: $mb_isActive2) {
                    EmptyView() }
                .buttonStyle(PlainButtonStyle())
        }.disabled(true))
        .highPriorityGesture(TapGesture(count: 2).onEnded {
            self.mb_isActive2 = true
        })
        .onTapGesture(count: 1) {
            self.mb_isActive1 = true
        }
    }
}

这篇关于SwitUI-列表中有两个NavigationLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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