I made a view to drag left and right in SwiftUI. This is working fine but having an issue when dragging quickly.
I made a Tinder like swipe in my SwiftUI app. When I tries to quickly drag the view left and right the view stucks there. I am attaching video :
I do a little bit of debugging and came to know that sometimes when I quickly try to drag the view only .onChange state is working, .onEnded never gets called. This happens only if I have my View in ScrollView. If there is no ScrollView, then it works perfectly fine.
This is my code:
struct SwipeCard : View {
@State var offset : CGFloat = 0
@GestureState var isDragging = false
var body: some View {
ScrollView{
MyView()
}
.cornerRadius(20)
.offset(x: offset)
.rotationEffect(.init(degrees: getRotation(angle: 30)))
.contentShape(RoundedCorner(radius: 20, corners: .allCorners))
.gesture(
DragGesture()
.updating($isDragging, body: { value, out, _ in
out = true
})
.onChanged({ value in
let translation = value.translation.width
offset = (isDragging ? translation : .zero)
if translation > -15 && translation < 15 {
withAnimation {
offset = .zero
}
}
})
.onEnded({ value in
print("drag gesture reaches on ended state")
// rest of the logic
})
)
}
}
I want that it should not stuck in the midway, it should make the offset to .zero. How can I achieve this?
I searched this issue a lot but nothing worked in my case.
I had tried this solution but it is not working in my case.