SwiftUI drag gesture freezes with multi-touch
Asked Answered
B

2

7

I am trying to drag a view in SwiftUI.

The drag works perfectly until I place my second finger on screen, after that the drag stops and neither of the code blocks (onChanged, onEnded) gets called.

But, when I again start dragging with a single finger, it again starts working.

Is there any work around to fix this or I am missing something?

struct Pager: View {
  func drag(geometry: GeometryProxy) -> some Gesture {
    DragGesture()
    .onChanged({ (value) in
      //some code
    })
    .onEnded({ (value) in
      //some code
    })
  }

  var body: some View {
    GeometryReader { (geometry) in
      ZStack {
        ForEach(self.items.indices.reversed(), id: \.self) { index in
          Card(index: index, item: self.items[index])
            .offset(x: 0, y: self.offset(index: index, geometry: geometry))
            .animation(.linear)
        }
      }
      .background(Color.black)
      .gesture(self.drag(geometry: geometry))
    }
  }
}
Brackish answered 23/7, 2020 at 15:40 Comment(1)
Does this answer your question? Detect DragGesture cancelation in SwiftUIShoebill
B
2

This approach is pretty helpful :

Detect DragGesture cancelation in SwiftUI

So basically, you compose the drag gesture with pinch & rotation gestures(combine them using .simultaneously).

When either pinch or rotation gets triggered, you can use it to signal a cancellation for your drag gesture and update your state accordingly

Bola answered 25/10, 2020 at 13:42 Comment(0)
H
1

You can use the same solution as this post, as I believe it is the same underlying problem. The solution employs a GestureState, which (as stated in the linked post) is a temporary value, and returns back to its initial state (0, 0) when the gesture is completed/interrupted.

Your code should look something like this when using the GestureState (though you may be able to adapt it to use a separate drag method like in your original post):

struct Pager: View {
  @GestureState private var dragPosition = CGSize.zero

  var body: some View {
      ZStack {
        ForEach(self.items.indices.reversed(), id: \.self) { index in
          Card(index: index, item: self.items[index])
            .offset(x: 0, y: self.dragPosition.height) // This will now map the y offset of the card to the dragPosition GestureState. If you want the x offset use self.dragPosition.width
            .animation(.linear)
        }
      }
      .background(Color.black)
      .gesture(
          DragGesture()
             .updating($dragPosition) { (value, gestureState, transaction) in
                 gestureState = CGSize(width: value.location.x - value.startLocation.x, height: value.location.y - value.startLocation.y)
              }
              .onChanged { value in
                  print("I'm changing")
              }
              .onEnded { value in
                  print("I've ended")
              }
      )
    }
}
Herrod answered 4/8, 2020 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.