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