.animation(_:value:) vs withAnimation

SwiftUI Mid-level Swift 5.5+ iOS 15+ Reviewed By Alok Singh

Predict the output

The user taps Toggle. What animates?

@State private var expanded = false

VStack {
    Rectangle()
        .frame(height: expanded ? 200 : 50)
        .animation(.spring, value: expanded)
    Text(expanded ? "Less" : "More")
        .font(expanded ? .title : .body)
    Button("Toggle") { expanded.toggle() }
}
Options
Hint 1 of 1

Which view has the animation modifier?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: B Only the rectangle's height

Correct. The modifier animates changes in the view it is attached to.

Why not the others?
  • A Both the rectangle and the text. That needs withAnimation { expanded.toggle() }.
  • C Nothing, because the animation isn't inside withAnimation. The value-based modifier animates on its own when expanded changes.
  • D Only the text. The animation modifier sits on the rectangle.

30-second answer

SwiftUI animates the difference between two states. withAnimation { } animates every change caused by that state update. .animation(.spring, value: x) animates changes inside the view it is attached to, but only when x changes. Transitions animate views being inserted or removed.

Practise saying it · 30 s. No recording, just a timer

Explain implicit vs explicit animation.

Say your answer out loud, then check which of these points you covered:

  • withAnimation animates the whole update
  • .animation(value:) scopes to a view
  • Respect Reduce Motion

Deep dive

The old .animation(_:) without value: is deprecated because it animated unrelated changes.

Custom animatable views implement animatableData or use the Animatable macro (iOS 26).

matchedGeometryEffect animates a view moving between places.

Honour Reduce Motion with @Environment(\.accessibilityReduceMotion).

Common mistakes

  • Using the deprecated .animation(_:).
  • Expecting a transition without an animation on the state change.
  • Ignoring Reduce Motion.

Senior take & follow-ups

Seniors explain animation as interpolation between states, and mention Reduce Motion unprompted.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you animate a view appearing?

    Show hint

    A .transition plus a state change inside withAnimation.

  2. How do you animate a custom shape?

    Show hint

    Implement animatableData.

Suggest a fix if something here is wrong or out of date.