SwitUI - 列表中的两个导航链接 [英] SwitUI - Two navigationLink in a list

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

问题描述

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

I have two NavigationLink in a cell of my List

我想点一次就去destination1,点两次就去destination2.所以我添加了两个点击手势来控制导航.

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 后面,它们也会自动激活.真正的效果是:点击单元格 ->前往目的地 1->回到家->前往目的地 2 ->回家

这是我的代码:

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
Double 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
Double 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 - 列表中的两个导航链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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