<!--
{
  "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/AnimationContext/state",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI16AnimationContextV5stateAA0C5StateVyxGvp"
  },
  "title" : "state"
}
-->

# state

The current state of a custom animation.

```
var state: AnimationState<Value>
```

## Discussion

An instance of [`CustomAnimation`](/documentation/SwiftUI/CustomAnimation) uses this property to read and
write state values as the animation runs.

An alternative to using the `state` property in a custom animation is
to create an [`AnimationStateKey`](/documentation/SwiftUI/AnimationStateKey) type and extend [`AnimationContext`](/documentation/SwiftUI/AnimationContext)
with a custom property that returns the state as a custom type. For
example, the following code creates a state key named `PausableState`.
It’s convenient to store state values in the key type, so the
`PausableState` structure includes properties for the stored state
values `paused` and `pauseTime`.

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

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

To provide access to the pausable state, the following code extends
`AnimationContext` to include the `pausableState` property. This
property returns an instance of the custom `PausableState` structure
stored in [`state`](/documentation/SwiftUI/AnimationContext/state), and it can also store a new
`PausableState` instance in `state`.

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

Now a custom animation can use the `pausableState` property instead of
the [`state`](/documentation/SwiftUI/AnimationContext/state) property as a convenient way to read and
write state values as the animation runs.

```
struct PausableAnimation: CustomAnimation {
    func animate<V>(value: V, time: TimeInterval, context: inout AnimationContext<V>) -> V? where V : VectorArithmetic {
        let pausableState = context.pausableState
        var pauseTime = pausableState.pauseTime
        ...
    }
}
```

---

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)