跳转至
Structure

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> : DynamicProperty

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 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)
    }
}

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Initializer

init(initialValue:) Creates a view state that's derived from a gesture with an initial value.

init(initialValue:reset:) Creates a view state that's derived from a gesture with an initial state value and a closure that provides a transaction to reset it.

init(initialValue:resetTransaction:) Creates a view state that's derived from a gesture with an initial state value and a transaction to reset it.

init(reset:) Creates a view state that's derived from a gesture with a closure that provides a transaction to reset it.

init(resetTransaction:) Creates a view state that's derived from a gesture with a transaction to reset it.

init(wrappedValue:) Creates a view state that's derived from a gesture.

init(wrappedValue:reset:) Creates a view state that's derived from a gesture with a wrapped state value and a closure that provides a transaction to reset it.

init(wrappedValue:resetTransaction:) Creates a view state that's derived from a gesture with a wrapped state value and a transaction to reset it.


Instance Property

projectedValue A binding to the gesture state property.

wrappedValue The wrapped value referenced by the gesture state property.