<!--
{
  "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/AnimationState/subscript(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Subscript",
  "symbol" : {
    "kind" : "Instance Subscript",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14AnimationStateVy5ValueQyd__qd__mcAA0cD3KeyRd__luip"
  },
  "title" : "subscript(_:)"
}
-->

# subscript(_:)

Accesses the state for a custom key.

```
subscript<K>(key: K.Type) -> K.Value where K : AnimationStateKey { get set }
```

## Overview

Create a custom animation state value by defining a key that conforms
to the [`AnimationStateKey`](/documentation/SwiftUI/AnimationStateKey) protocol and provide the
[`defaultValue`](/documentation/SwiftUI/AnimationStateKey/defaultValue) for the key. Also include properties
to read and write state values that your [`CustomAnimation`](/documentation/SwiftUI/CustomAnimation) uses. For
example, the following code defines a key named `PausableState` that
has two state values, `paused` and `pauseTime`:

```
private struct PausableState<Value: VectorArithmetic>: AnimationStateKey {
    var paused = false
    var pauseTime: TimeInterval = 0.0

    static var defaultValue: Self { .init() }
}
```

Use that key with the subscript operator of the [`AnimationState`](/documentation/SwiftUI/AnimationState)
structure to get and set a value for the key. For more convenient
access to the key value, extend [`AnimationContext`](/documentation/SwiftUI/AnimationContext) with a computed
property that gets and sets the key’s value.

```
extension AnimationContext {
    fileprivate var pausableState: PausableState<Value> {
        get { state[PausableState<Value>.self] }
        set { state[PausableState<Value>.self] = newValue }
    }
}
```

To access the state values in a [`CustomAnimation`](/documentation/SwiftUI/CustomAnimation), call the custom
computed property, then read and write the state values that the
custom [`AnimationStateKey`](/documentation/SwiftUI/AnimationStateKey) provides.

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

---

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)