简体   繁体   English

SwiftUI onTapGesture 块 NavigationLink 动作

[英]SwiftUI onTapGesture block NavigationLink action

I want to press a cell of List to display a menu then click the menu push to a new view by NavigationLink .我想按List的一个单元格来显示一个菜单,然后单击菜单通过NavigationLink推送到一个新视图。

在此处输入图像描述

Here is my code:这是我的代码:

import SwiftUI

struct ContentView: View {
    let array = ["a", "b"]

    @State var isPushed: Bool = false

    @State var isDisplayMenu: Bool = false
    @State var isDestination1Clicked: Bool = false
    var body: some View {
        NavigationView {
            List {
                ForEach(array, id:\.self) { value in
                    VStack {
                        RowView(title: value)
                         .frame(maxWidth: .infinity)
                        if isDisplayMenu {
                            HStack {
                                ZStack {
                                    Text("D1")
                                    NavigationLink(
                                        destination: Destination1(),
                                        label: {
                                            EmptyView()}).hidden()
                                }

                                ZStack {
                                    Text("D2")
                                    NavigationLink(
                                        destination: Destination2(),
                                        label: {
                                            EmptyView()}).hidden()
                                }
                            }

                        }
                    }
                    .background(Color.green)
                    .contentShape(Rectangle())
                    .onTapGesture(count: 1, perform: {
                        isDisplayMenu.toggle()
                    })
                }
            }
        }

    }
}

struct Destination1: View {
    var body: some View {
        Text("D1")
    }
}

struct Destination2: View {
    var body: some View {
        Text("D2")
    }
}

struct RowView: View {

    var title: String

    var body: some View {
        VStack {
            Text("title")
            Text(title)
        }.background(Color.red)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

When click the cell the D1 and D2 was displayed.单击单元格时,将显示 D1 和 D2。 But the NavigationLink doesn't work.但是NavigationLink不起作用。

I think the reason is onTapGesture blocked it.我认为原因是onTapGesture阻止了它。 How can I make the NavigationLink works?我怎样才能使NavigationLink工作?

Any help?有什么帮助吗? thanks!谢谢!

There are some restriction defined in List to be just a List itself. List 中定义的一些限制只是一个 List 本身。

So that we cannot use more than one NavigationLink inside a List cell ( If we use more than one, we cannot get expected behaviour)这样我们就不能在 List 单元格中使用多个 NavigationLink (如果我们使用多个,则无法获得预期的行为)

And also Empty views does not get touch events.而且 Empty views 也不会获得触摸事件。

Here is the fixes for your code.这是您的代码的修复程序。

struct ContentView: View {
    let array = ["a", "b"]
    
    @State var isPushed: Bool = false
    
    @State var isDisplayMenu: Bool = true
    @State var isDestination1Clicked: Bool = false
    var body: some View {
        NavigationView {
            ScrollView {
                ForEach(array, id:\.self) { value in
                    VStack {
                        RowView(title: value)
                            .frame(maxWidth: .infinity)
                            .background(Color.green)
                            .onTapGesture(count: 1, perform: {
                                withAnimation {
                                    isDisplayMenu.toggle()
                                }
                            })
                        
                        if isDisplayMenu {
                            HStack {
                                
                                NavigationLink(
                                    destination: Destination1(),
                                    label: {
                                        Spacer()
                                        Text("D1")
                                            .foregroundColor(.black)
                                        Spacer()
                                    })
                                
                                NavigationLink(
                                    destination: Destination2(),
                                    label: {
                                        Spacer()
                                        Text("D2")
                                        Spacer()
                                    })
                                    .foregroundColor(.black)
                                
                            }
                            .background(Color.purple)
                            
                        }
                    }
                    .padding()
                    .contentShape(Rectangle())
                    
                }
            }
        }
        
    }
}

struct Destination1: View {
    var body: some View {
        Text("D1")
    }
}

struct Destination2: View {
    var body: some View {
        Text("D2")
    }
}

struct RowView: View {
    
    var title: String
    
    var body: some View {
        VStack {
            Text("title")
            Text(title)
        }.background(Color.red)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

try to attach the "onTapGesture" to the "NavigationView".尝试将“onTapGesture”附加到“NavigationView”。

NavigationView {
  ....
 }.onTapGesture(count: 1, perform: {
        isDisplayMenu.toggle()
 })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM