Any SwiftUI Button equivalent to UIKit's "touch down", i.e. activate button when your finger touches?
You can use a DragGesture with a minimumDistance of zero and define a closure for DOWN (onChanged()) or UP (onEnded()):
struct ContentView: View {
@State private var idx = 0
var body: some View {
let g = DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged({
print("DOWN: \($0)")
}).onEnded({
print("UP: \($0)")
})
return Rectangle().frame(width: 100, height: 50).gesture(g)
}
}
You can create a custom view modifier:
extension View {
func onTouchDownGesture(callback: @escaping () -> Void) -> some View {
modifier(OnTouchDownGestureModifier(callback: callback))
}
}
private struct OnTouchDownGestureModifier: ViewModifier {
@State private var tapped = false
let callback: () -> Void
func body(content: Content) -> some View {
content
.simultaneousGesture(DragGesture(minimumDistance: 0)
.onChanged { _ in
if !self.tapped {
self.tapped = true
self.callback()
}
}
.onEnded { _ in
self.tapped = false
})
}
}
struct MyView: View {
var body: some View {
Text("Hello World")
.onTouchDownGesture {
print("View did tap!")
}
}
}