I\'m trying to detect when a finger first makes contact with a view in SwiftUI. I could do this very easily with UIKit Events but can\'t figure this out in SwiftUI.
I
DragGesture was a bit unreliable for me, but LongPressGesture works perfectly. Here is a way to call methods when the GestureState value changes:
@GestureState var longPressGestureState = false
var isTouchedDown: Bool {
// use this place to call functions when the value changes
return longPressGestureState
}
var body: View {
Color(isTouchedDown ? .red : .black)
.gesture(
LongPressGesture(minimumDuration: .infinity, maximumDistance: .infinity)
.updating($longPressGestureState) { value, state, _ in
state = value
}
)
}