SwiftUI 在 NavigationLink 上滑动/拖动

标签 swift swiftui iso navigationview swiftui-navigationlink

我希望能够通过在其中包含多个 NavigationLinks 的 NavigationView 上向左/向右滑动来更新 View 。 NavigationLinks 转到不同的演示 View 。
当我在最外面的 View 上开始拖动手势时,如果该手势从 NavigationLink 开始,则链接会更改颜色,就像它已被按下一样。继续拖动手势,主 View 确实按预期更新,NavigationLink 返回到它的正常状态(颜色)。
我需要的是一种让 NavigationLink 在发生拖动手势时不改变颜色的方法。也许是一种让 NavigationLink react “如果”触摸是长触摸或其他东西的方法。
这是一些演示我所看到的代码。这不是我的实际项目,而是一个非常精简的示例。
任何建议或解决方案表示赞赏!

import SwiftUI

struct ContentView: View {
    
    @State var outputText: String = ""
    var body: some View {
        VStack(alignment: .center, spacing: 20) {
            NavigationView {
                VStack {
                    Text("Navigation View")
                    NavigationLink(destination: Text("Showwing Widget")) {
                        HStack {
                            Text("Navigation Link")
                        }
                        .frame(width: 300, height: 200)
                        .border(Color.blue)
                    }
                    .border(Color.red)
                    Spacer()
                }
                .border(Color.yellow)
            }

            Text(outputText)
                .font(.title)
                .fontWeight(.bold)

            Text("My Green Oval")
                .foregroundColor(.white)
                .fontWeight(.bold)
                .font(.title)
                .frame(width: 300, height: 200)
                .background(
                    Ellipse()
                        .fill(Color.green)
                )
            Button(action: {
                outputText = "Button tapped"
            }) {
                Text("Button to Tap")
            }
            
            Text("Just some words...")
            Spacer()
        }
        .highPriorityGesture(DragGesture(minimumDistance: 25, coordinateSpace: .local)
            .onEnded { value in
                if abs(value.translation.height) < abs(value.translation.width) {
                    if abs(value.translation.width) > 50.0 {
                        if value.translation.width < 0 {
                            self.swipeRightToLeft()
                        } else if value.translation.width > 0 {
                            self.swipeLeftToRight()
                        }
                    }
                }
            }
        )
    }
    
    func swipeRightToLeft() {
        outputText = "Swiped Right to Left <--"
    }
    
    func swipeLeftToRight() {
        outputText = "Swiped Left to Right -->"
    }
}

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

最佳答案

在永远玩弄之后,我在这里有一个解决方案! Hacky 也许,但它的工作原理很少。使用以抽屉行是否打开为条件的偏移量配置您的 View ,并创建一个状态变量以跟踪您的用户是否正在拖动。您可以使用 UIScreen.main.bounds.width 获取 screenWidth。

.offset(x: self.isOpen ? -screenWidth/12 : 0, y: 0)
                .simultaneousGesture(DragGesture()
                            .onChanged{ gesture in
                                self.isDragging = true
                                self.offset = gesture.translation.width
                            }
                            .onEnded { _ in
                                self.isDragging = false
                                if self.offset > 0 {
                                    withAnimation {
                                        self.isOpen = false
                                        self.offset = 0
                                    }
                                } else if self.offset < 0 {
                                    withAnimation {
                                        self.isOpen = true
                                        self.offset = 0
                                    }
                                }
                            })
然后在您的 NavigationLink 上添加此禁用修饰符
.disabled(self.isDragging || self.isOpen)
祝你好运!如果您还没有找到解决方案,希望这也适用于您。

关于SwiftUI 在 NavigationLink 上滑动/拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65206356/

相关文章:

ios - 在 Tab Bar 前面制作 UIPickerView

python - 将 JSON 日期字符串转换为 Python 日期时间

xml - £ 变成 £ 为什么? XML ISO 编码问题?

asp.net-mvc-3 - 下载ISO文件

ios - 快速将图像添加到工具栏?

ios - 如何更改 CAShapeLayer alpha/opacity 值?

ios - iOS Swift 2.0 : Use of unsolved identifier 'UITableviewCell'

ios - SwiftUI 以异步方式加载很长的列表

swiftui - 是否可以使用 SwiftUI 在 TextField 上设置字符限制?

swift - “链接”仅在 macOS 11.0 或更高版本中可用