Contents

keyframeAnimator(initialValue:trigger:content:keyframes:)

Plays the given keyframes when the given trigger value changes, updating the view using the modifiers you apply in body.

Declaration

nonisolated func keyframeAnimator<Value>(initialValue: Value, trigger: some Equatable, @ViewBuilder content: @escaping  @Sendable (PlaceholderContentView<Self>, Value) -> some View, @KeyframesBuilder<Value> keyframes: @escaping (Value) -> some Keyframes) -> some View

Parameters

  • initialValue:

    The initial value that the keyframes will animate from.

  • trigger:

    A value to observe for changes.

  • content:

    A view builder closure that takes two parameters. The first parameter is a proxy value representing the modified view. The second parameter is the interpolated value generated by the keyframes.

  • keyframes:

    Keyframes defining how the value changes over time. The current value of the animator is the single argument, which is equal to initialValue when the view first appears, then is equal to the end value of the previous keyframe animation on subsequent calls.

Discussion

Note that the content closure will be updated on every frame while animating, so avoid performing any expensive operations directly within content.

If the trigger value changes while animating, the keyframes closure will be called with the current interpolated value, and the keyframes that you return define a new animation that replaces the old one. The previous velocity will be preserved, so cubic or spring keyframes will maintain continuity from the previous animation if they do not specify a custom initial velocity.

When a keyframe animation finishes, the animator will remain at the end value, which becomes the initial value for the next animation.

See Also

Creating keyframe-based animation