简体   繁体   English

NavigationLink + Text.onTapGesture 碰撞

[英]NavigationLink + Text.onTapGesture Colliding

This problem is that when clicking upon the Text in the view for a NavigationLink, the onTapGesture is getting priority and ignoring the NavigationLink.这个问题是,当单击 NavigationLink 视图中的文本时,onTapGesture 获得优先级并忽略 NavigationLink。 I have tried on simultaneousGesture , but am still having issues.我已经尝试过simultaneousGesture ,但仍然遇到问题。

Here is the NavigationView/List/NavigationLink bit of code:这是 NavigationView/List/NavigationLink 的代码:

NavigationView {
            List {
                ForEach(gameStore.games) { game in                    
                    NavigationLink(destination: EmojiMemoryGameView(viewModel: game)
                                    .navigationBarTitle(self.gameStore.name(for: game))
                    ) {
                        EmojiThemeEditorView(game: game, isEditing: self.editMode.isEditing)
                    }                    
                }
                .onDelete { indexSet in
                    indexSet.map { self.gameStore.games[$0] }
                        .forEach { game in
                            self.gameStore.removeGameTheme(game)
                        }
                }
            }
            .navigationBarTitle(self.gameStore.name)
            .navigationBarItems(
                leading: Button(action: {
                    self.gameStore.addGameTheme()
                }, label: {
                    Image(systemName: "plus").imageScale(.large)
                }),
                trailing: EditButton()
            )
            .environment(\.editMode, $editMode)

And here is the EmojiThemeEditorView 's body:这是EmojiThemeEditorView的正文:

var body: some View {
        Text(game.theme.themeName).foregroundColor(game.theme.colorToUse).font(.largeTitle)
//            .simultaneousGesture(TapGesture().onEnded { // tried this and did not work
            .onTapGesture {
                if (isEditing) {
                    self.showThemeEditor = true
                }
            }
            .popover(isPresented: $showThemeEditor) {
                EmojiThemeEditorOptionView(isShowing: $showThemeEditor).environmentObject(self.game.theme)
            }
    }

Basically, clicking on the Text, does not transition to the NavigationLink.基本上,单击文本不会转换到 NavigationLink。 Here is an example:这是一个例子:

在此处输入图像描述

Any thoughts on the best way to navigate around this issue?有关解决此问题的最佳方法的任何想法?

I've had similar issues with navigation links being wonky.我也遇到过类似的导航链接不稳定的问题。 I've had the best success treating navigation links as if they are things that should not go in lists and relying on the isActive flag to trigger them.我在处理导航链接方面取得了最大的成功,就好像它们不应该出现在列表中 go 并依靠 isActive 标志来触发它们。

Here's a good tutorial on how to use isActive:这是一个关于如何使用 isActive 的好教程:

https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui

I suspect changing your navigationLinks to Buttons, making one NavigationLink in your view, and having your onTapGesture change a state object for the game and change a Bool for isActive will work.我怀疑将您的 navigationLinks 更改为按钮,在您的视图中创建一个 NavigationLink,并让您的 onTapGesture 为游戏更改 state object 并为 isActive 更改 Bool 将起作用。 It's a weird paradigm, very different from html links.这是一个奇怪的范例,与 html 链接非常不同。

The key is关键是

var body: some View {
@State var emojiView = false
    NavigationLink(“”, destination: EmojiMemoryGameView(viewModel: game), isActive: self.$emojiView)

If you change the self.emojiView in your onTapGesture and the game variable in same place, it should navigate you to the link.如果您更改 onTapGesture 中的 self.emojiView 和同一位置的游戏变量,它应该会将您导航到链接。 Hope that makes sense.希望这是有道理的。

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

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