【问题标题】:DragGesture breaking sheet's swipe gestureDragGesture 打破工作表的滑动手势
【发布时间】:2021-03-05 01:07:17
【问题描述】:

我遇到了一个问题,如果我将 DragGesture 添加到应该作为工作表模式呈现的整个视图,而不是通过拖动来解除工作表的默认行为不起作用。这是我编写的用于演示此错误的示例代码:

private extension UIApplication {
    func dismissFirstResponder() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

struct DragGestureExample: View {
    var body: some View {
        ZStack {
            Color.gray
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            VStack(alignment: .center){
                TextField("Type Text Here", text: .constant(""))
                    .padding()
                Spacer()
                Text("Hello, World!")
                    .padding()
            }
        }
        .gesture(
            DragGesture()
                .onChanged { _ in
                    UIApplication.shared.dismissFirstResponder()
                })
    }
}

这是调用视图的 ContentView:

struct ContentView: View {
    @State var isShown: Bool = false
    var body: some View {
        VStack{
            Button("Click for sheet") {
                self.isShown.toggle()
            }
            .sheet(isPresented: $isShown) {
                DragGestureExample()
            }
        }
    }
}

关闭键盘的拖动手势有效,但它破坏了工作表的默认拖动行为。如果我删除拖动手势,那么我可以拖动以再次关闭工作表。有没有优先考虑工作表的手势?

【问题讨论】:

    标签: ios swift xcode swiftui gesture


    【解决方案1】:

    您的 DragGestureExample 正在占用 sheet 的所有屏幕,因此 sheet 将作为其自身进入背景,并且由于您获得了 DragGesture DragGestureExample 的所有屏幕,那么对于工作表拖动解除功能就没有更多工作要做了,因为您正在将它用于您自己的代码! 有一种方法可以两者兼得!您可以在您喜欢的逻辑中将关闭工作表代码放在 DragGestureExample 的 DragGesture 上,然后您可以再次使用这两个拖动!

    例如,在您关闭键盘或您的特殊工作后,也可以设置关闭工作表的条件,例如,如果转换大于某个值,则关闭工作表!甚至先抵消然后完全消除它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多