<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/AnimationStateKey",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI17AnimationStateKeyP"
  },
  "title" : "AnimationStateKey"
}
-->

# AnimationStateKey

A key for accessing animation state values.

```
protocol AnimationStateKey
```

## Overview

To access animation state from an [`AnimationContext`](/documentation/SwiftUI/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`](/documentation/SwiftUI/AnimationStateKey/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`](/documentation/SwiftUI/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`](/documentation/SwiftUI/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`](/documentation/SwiftUI/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
    }
}
```

## Topics

### Setting the default value

[`static var defaultValue: Self.Value`](/documentation/SwiftUI/AnimationStateKey/defaultValue)

The default value for the animation state key.

[`associatedtype Value`](/documentation/SwiftUI/AnimationStateKey/Value)

The associated type representing the type of the animation state key’s
value.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)