<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI16AnimationContextV"
  },
  "title" : "AnimationContext"
}
-->

# AnimationContext

Contextual values that a custom animation can use to manage state and
access a view’s environment.

```
struct AnimationContext<Value> where Value : VectorArithmetic
```

## Overview

The system provides an `AnimationContext` to a [`CustomAnimation`](/documentation/SwiftUI/CustomAnimation) instance
so that the animation can store and retrieve values in an instance of
[`AnimationState`](/documentation/SwiftUI/AnimationState). To access these values, use the context’s
[`state`](/documentation/SwiftUI/AnimationContext/state) property.

For more convenient access to state, create an [`AnimationStateKey`](/documentation/SwiftUI/AnimationStateKey) and
extend `AnimationContext` to include a computed property that gets and
sets the [`AnimationState`](/documentation/SwiftUI/AnimationState) value. Then use this property instead of
[`state`](/documentation/SwiftUI/AnimationContext/state) to retrieve the state of a custom animation. For
example, the following code creates an animation state key named
`PausableState`. Then the code extends `AnimationContext` to include the
`pausableState` property:

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

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

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

To access the pausable state, the custom animation `PausableAnimation` uses
the `pausableState` property instead of the [`state`](/documentation/SwiftUI/AnimationContext/state)
property:

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

The animation can also retrieve environment values of the view that created
the animation. To retrieve a view’s environment value, use the context’s
[`environment`](/documentation/SwiftUI/AnimationContext/environment) property. For instance, the following code
creates a custom [`EnvironmentValues`](/documentation/SwiftUI/EnvironmentValues) property named `animationPaused`, and the
view `PausableAnimationView` uses the property to store the paused state:

```
extension EnvironmentValues {
    @Entry var animationPaused: Bool = false
}

struct PausableAnimationView: View {
    @State private var paused = false

    var body: some View {
        VStack {
            ...
        }
        .environment(\.animationPaused, paused)
    }
}
```

Then the custom animation `PausableAnimation` retrieves the paused state
from the view’s environment using the [`environment`](/documentation/SwiftUI/AnimationContext/environment)
property:

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

## Topics

### Managing state

[`var state: AnimationState<Value>`](/documentation/SwiftUI/AnimationContext/state)

The current state of a custom animation.

### Retrieving view environment values

[`var environment: EnvironmentValues`](/documentation/SwiftUI/AnimationContext/environment)

The current environment of the view that created the custom animation.

### Creating context

[`func withState<T>(AnimationState<T>) -> AnimationContext<T>`](/documentation/SwiftUI/AnimationContext/withState(_:))

Creates a new context from another one with a state that you provide.

### Instance Properties

[`var isLogicallyComplete: Bool`](/documentation/SwiftUI/AnimationContext/isLogicallyComplete)

Set this to `true` to indicate that an animation is logically complete.

---

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)