I need a scrolling view to report when it's scroll/drag gesture ends.
Here's a simple SwiftUI example taking a basic approach:
import SwiftUI
struct CarouselView: View {
let colors: [Color] = [.red, .green, .blue]
var drag: some Gesture {
DragGesture()
.onChanged { state in
print("changing")
}
.onEnded { state in
print("ended")
}
}
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(0..<10) { i in
Text("Block \(i)")
.frame(width: 300, height: 300)
.background(colors[i % colors.count])
.id(i)
}
}
}
.gesture(drag)
}
}
The gesture's events properly fire when you drag vertically, but when you drag horizontally (matching the direction of the scroll view) only the first onChanged event fires.
I don't want to even begin trying to reimplement a ScrollView in SwiftUI just to get these events, but is there a better way?