I want to detect when the user stopped touching the screen. But I want it to be in a vertical ScrollView and a DragGesture isn't recognized when the view is scrolled vertically. I'm guessing this is because there wasn't anything dragged, since the view moved along with the user's finger.
import SwiftUI
struct TestView: View {
var body: some View {
ScrollView {
VStack(spacing: 0) {
Rectangle()
.foregroundStyle(.green)
.frame(height: 700)
}
}
.gesture(DragGesture().onEnded({ _ in print("Drag gesture ended") }))
}
}
How should I go about detecting when the user lifted their finger off the screen on a scrollview?
SwiftUI ScrollView does not have an API for this information. You can use Feedback Assistant to file an enhancement request for such an API. Please include an explanation of what you're trying to accomplish if you open the enhancement request.
As a workaround, you could use a UIScrollView and wrap the scroll view in a UIViewRepresentable
. UIScrollView
allows you subclass touchesShouldBegin(_:with:in:) and touchesShouldCancel(in:) methods (which the scroll view calls) to affect how the scroll view handles scrolling gestures. Also you could use UIScrollViewDelegate
method scrollViewDidEndDragging(_:willDecelerate:)
which tells the delegate when dragging ended in the scroll view.