AnimationStateKey
protocol · iOS 17.0+ · SDK 27.1
protocol AnimationStateKeyA key for accessing animation state values. To access animation state from an ``AnimationContext`` in a custom animation, create an `AnimationStateKey`. For example, the following code creates an animation state key named `PausableState` and sets the value for the required ``defaultValue`` property. The code also defines properties for state values that the custom animation needs when calculating animation values. Keeping the state values in the animation state key makes it more convenient to read and write those values in the implementation of a ``CustomAnimation``. private struct PausableState<Value: VectorArithmetic>: AnimationStateKey { var paused = false var pauseTime: TimeInterval = 0.0 static var defaultValue: Self { .init() } } To make accessing the value of the animation state key more convenient, define a property for it by extending ``AnimationContext``: extension AnimationContext { fileprivate var pausableState: PausableState<Value> { get { state[PausableState<Value>.self] } set { state[PausableState<Value>.self] = newValue } } } Then, you can read and write your state in an instance of `CustomAnimation` using the ``AnimationContext``: struct PausableAnimation: CustomAnimation { let base: Animation func animate<V>(value: V, time: TimeInterval, context: inout AnimationContext<V>) -> V? where V : VectorArithmetic { let paused = context.environment.animationPaused let pausableState = context.pausableState var pauseTime = pausableState.pauseTime if pausableState.paused != paused { pauseTime = time - pauseTime context.pausableState = PausableState(paused: paused, pauseTime: pauseTime) } let effectiveTime = paused ? pauseTime : time - pauseTime let result = base.animate(value: value, time: effectiveTime, context: &context) return result } }
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
No declarations were skipped in this snapshot.