GestureState
A property wrapper type that updates a property while the user performs a gesture and resets the property back to its initial state when the gesture ends.
Declaration
@propertyWrapper @frozen struct GestureState<Value>Mentioned in
Overview
Declare a property as @GestureState, pass as a binding to it as a parameter to a gesture’s updating(_:body:) callback, and receive updates to it. A property that’s declared as @GestureState implicitly resets when the gesture becomes inactive, making it suitable for tracking transient state.
Add a long-press gesture to a Circle, and update the interface during the gesture by declaring a property as @GestureState:
struct SimpleLongPressGestureView: View {
@GestureState private var isDetectingLongPress = false
var longPress: some Gesture {
LongPressGesture(minimumDuration: 3)
.updating($isDetectingLongPress) { currentState, gestureState, transaction in
gestureState = currentState
}
}
var body: some View {
Circle()
.fill(self.isDetectingLongPress ? Color.red : Color.green)
.frame(width: 100, height: 100, alignment: .center)
.gesture(longPress)
}
}Topics
Creating a gesture state
init(initialValue:)init(initialValue:reset:)init(initialValue:resetTransaction:)init(reset:)init(resetTransaction:)init(wrappedValue:)init(wrappedValue:reset:)init(wrappedValue:resetTransaction:)