animation

Applies the given animation to all animatable values within the body closure.

View ModifieriOS 17.0+macOS 14.0+tvOS 17.0+watchOS 10.0+

Overloads

2 ways to call compose().animation(). Each is one SwiftUI overload, picked by the arguments you pass.

React
compose().animation(
  animation: SwiftUI.Animation | null,
  options: {
    body: ViewModifierChain;
  }
)
Swift
nonisolated func animation<V>(
    _ animation: Animation?,
    @ContentBuilder body: (PlaceholderContentView<Self>) -> V
) -> some View
where V : View

Parameters

Discussion

Any modifiers applied to the content of body will be applied to this view, and the animation will only be used on the modifiers defined in the body.

The following code animates the opacity changing with an easeInOut animation, while the contents of MyView are animated with the implicit transaction's animation:

MyView(isActive: isActive)
    .animation(.easeInOut) { content in
        content.opacity(isActive ? 1.0 : 0.0)
    }

See also

More in Animation.