2021年3月4日星期四

DragGesture breaking sheet's swipe gesture

I'm experiencing a problem where if I add a DragGesture to a whole view that is supposed to be presented as a sheet modal than the default behavior of dismissing the sheet by dragging it doesn't work. Here's an example code I wrote up to demonstrate this bug:

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()                  })      }  }  

And here's my ContentView that calls the view:

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

The drag gesture to dismiss the keyboard works, however it breaks the default drag behavior of the sheet. If I remove the drag gesture then I am able to drag to dismiss the sheet again.

https://stackoverflow.com/questions/66485297/draggesture-breaking-sheets-swipe-gesture March 05, 2021 at 09:07AM

没有评论:

发表评论