Hi there, I'm playing with SwiftUI and trying to implement a scrolling view with buttons that adjust their transform on touchDown and touchUp.
I'm using a DragGesture with a minimum distance of 0 to implement touchDown and touchUp, and finding that it blocks scrolling. My approach might be wrong here, so wondering how to best accomplish what I'm trying to do.
Here's a Playground snippet that reproduces my issue
import SwiftUI
import PlaygroundSupport
public extension View {
func onTouchDown(
_ onTouchDown: @escaping () -> Void,
onTouchUp: @escaping () -> Void) -> some View {
self.gesture(
DragGesture(minimumDistance: 0, coordinateSpace: .local)
.onChanged({ _ in onTouchDown() })
.onEnded({ _ in onTouchUp()})
)
}
}
struct ContentView: View {
var body: some View {
GeometryReader { geo in
ScrollView {
VStack(spacing: 50) {
Rectangle()
.frame(width: geo.size.width, height: 100)
.onTouchDown({
print("touch down")
}) {
print("touch up")
}
Rectangle()
.frame(width: geo.size.width, height: 100)
.onTouchDown({
print("touch down")
}) {
print("touch up")
}
Rectangle()
.frame(width: geo.size.width, height: 100)
.onTouchDown({
print("touch down")
}) {
print("touch up")
}
Rectangle()
.frame(width: geo.size.width, height: 100)
.onTouchDown({
print("touch down")
}) {
print("touch up")
}
}.offset(y: 100)
}
}
}
}
PlaygroundPage.current.liveView = UIHostingController(
rootView: ContentView()
)
I'd like for my drag gestures on the subviews as well as the ScrollView's drag to be simultaneously recognized.