skip to content

In SwiftUI, what is the difference between animation(_:value:) and withAnimation, and which views animate with each?

level: middleimportance: must knowfreq 75%

answer

  1. where the animation is attached
  2. a view scope versus a change scope
  3. an Equatable value to watch
  4. the current Transaction's animation property

basics

~10 s

animation(_:value:) is a modifier that animates one view's subtree only when a watched Equatable value changes; withAnimation wraps a state mutation so every view whose rendering that mutation changes animates.

solid answer

~40 s

Both put an `Animation` on the update's `Transaction`; they differ in scope. `animation(_:value:)` is a view modifier: it animates the content and modifiers it wraps, and only in updates where the `Equatable` value it watches changed, whatever caused that change. `withAnimation(.snappy) { isExpanded.toggle() }` is a global function: it sets the animation on the current transaction while its closure runs, so every view whose rendering changes because of those mutations animates, anywhere in the hierarchy. I use the modifier when a view should always animate one piece of state, and `withAnimation` when one user action should animate everything it touches. The value-less `animation(_:)` on an arbitrary view was deprecated in iOS 15 because it animated changes nobody intended.

code

swift · 18 lines
swift
struct LikeButton: View {
    @State private var isLiked = false
    @State private var count = 41

    var body: some View {
        HStack {
            Image(systemName: isLiked ? "heart.fill" : "heart")
                .scaleEffect(isLiked ? 1.25 : 1.0)
                // Implicit: only this image, only when isLiked changes.
                .animation(.bouncy, value: isLiked)
            Text(count, format: .number)
        }
        .onTapGesture {
            isLiked.toggle()
            count += isLiked ? 1 : -1
        }
    }
}

go deeper

for a junior

Know both spellings and one example of each: animation(_:value:) on a view, withAnimation around a toggle in a button action.

for a middle

Explain the two scopes: the modifier reaches its wrapped subtree and fires on one value; withAnimation reaches every view the closure's mutations change. Mention Binding.animation(_:) as the third route.

for a senior

Show you can predict which view animates when both are present, keep implicit modifiers narrow, and replace the deprecated value-less form in an existing codebase without animating unrelated changes.

for a principal

Be ready to state a team convention, such as explicit animation in actions and value-scoped modifiers only on self-contained components, and why it keeps screens predictable.

## Two triggers for one mechanism SwiftUI never animates a view object. It animates the **difference between two renders**: when state changes, SwiftUI computes the new `body`, and the animatable values that differ from the previous render (a frame, an offset, a `scaleEffect`, an `opacity`, a shape's geometry) are interpolated if the update carries an animation. The carrier is a **`Transaction`**, which Apple's documentation calls "the context of the current state-processing update"; its `animation` property says which animation, if any, the update uses. The two everyday APIs differ only in **where** they put that animation and therefore in **what** they reach. ## `animation(_:value:)`: scoped to a view and a value `animation(_:value:)` is a view modifier. Apple describes it as applying "the given animation to this view when the specified value changes". That sentence carries two scopes: - **The view scope.** A modifier wraps the view in front of it, so the animation reaches the content and the modifiers written before it in the chain. Modifiers written after it, and sibling views, are outside this modifier's reach. - **The value scope.** `value` must be `Equatable`. The animation is applied in updates where that value changed, whichever button, gesture or model update changed it. A different state change that alters the same view is not animated by this modifier. - **`nil` means no animation.** `.animation(nil, value: x)` keeps the view from animating when `x` changes. This is the tool for a view that should always animate one particular piece of state: a like button's heart that pops whenever `isLiked` flips, wherever the flip came from. ## `withAnimation`: scoped to a state change `withAnimation(_:_:)` is a global function. Its documentation says it sets the given animation "as the animation property of the thread's current Transaction". While its closure runs, every mutation joins that transaction, so **every view whose rendering changes because of those mutations animates**, wherever it sits in the hierarchy. The animation parameter defaults to `.default`. - It is **explicit**: the animation follows the action, not the view. - The closure is synchronous (`() throws -> Result`, not `async`). A mutation made after it returns, for example after an `await` in a `Task`, belongs to a later update and is not covered. - Since iOS 17, `withAnimation(_:completionCriteria:_:completion:)` adds a completion callback. ## Comparing them | | `animation(_:value:)` | `withAnimation` | |---|---|---| | Where it is written | On a view, as a modifier | Around the mutation, in an action | | What it reaches | The wrapped subtree | Every view the mutation changes, unless a view below overrides it | | What triggers it | Any change to the watched value | That one call | | Typical use | A view that always animates one value | One action whose whole effect animates | There is a third route that Apple's Animations overview lists beside these two: a **binding's** `animation(_:)` method. `Toggle("Wi-Fi", isOn: $isOn.animation(.smooth))` animates the changes written through that binding, which is handy when a control, not your own code, performs the mutation. ## When both apply to one update The animation travels down the hierarchy inside the transaction, and a view lower down can change what the transaction carries for its own subtree. Apple's sample for `transaction(_:)` shows exactly this: three texts rotate in one `withAnimation(.easeIn(duration: 2.0))` change, one keeps the ease-in, one delays it, and one replaces it with a spring. A value-scoped modifier behaves the same way in practice: when its value changed in the update, its own animation is used for its subtree, so an inner `.animation(.bouncy, value: isLiked)` can give the heart a spring while the rest of the screen follows the outer curve. Since iOS 17, `animation(_:body:)` narrows this further, applying the animation only to the modifiers written inside its `body` closure. ## The deprecated form and the usual mistakes The value-less `animation(_:)` on an arbitrary view was deprecated in iOS 15 with the message "Use withAnimation or animation(_:value:) instead." It animated every change that reached the view, including ones nobody meant to animate, such as data reloading or a layout shift from the keyboard. (A separate value-less `animation(_:)` added in iOS 15 for views that are themselves `Equatable` is not deprecated; it watches the view's own value.) Mistakes interviewers listen for: 1. **Watching the wrong value.** `.animation(.bouncy, value: isLiked)` does not animate a change to `count`. 2. **Placing the modifier too early.** Written before the modifier whose value changes, it leaves that change outside its subtree. 3. **Wrapping a whole screen.** One value-scoped modifier on a root container animates every layout change in that subtree that happens in the same update as its value flipping, including ones that should snap. 4. **Expecting `withAnimation` to reach later code.** Work after an `await` runs in a different update with no animation unless it is wrapped again. The rule of thumb: put `withAnimation` where the user's intent is (the action), and `animation(_:value:)` where a view's own behaviour is (the view), and keep each as narrow as the effect you want.

  • Why was the value-less animation(_:) modifier deprecated, and what replaces it?
    On an arbitrary view it animated every change that reached the view, so unrelated updates such as a data reload or a keyboard layout shift animated too. It was deprecated in iOS 15 with the message "Use withAnimation or animation(_:value:) instead." Replace it with `animation(_:value:)` naming the value that should animate, or with `withAnimation` around the mutation in the action.
  • On iOS 17 and later, how do you animate only one modifier of a view rather than its whole subtree?
    Use `animation(_:body:)`. The animation applies only to the modifiers inside the closure: `MyView().animation(.easeInOut) { $0.opacity(isActive ? 1 : 0) }` fades with ease-in-out, while the rest of `MyView` uses whatever animation the update's transaction carries.
  • How can a Toggle animate the effect of its change when your code never calls withAnimation?
    Give it a binding with an animation: `Toggle("Wi-Fi", isOn: $isOn.animation(.smooth))`. `Binding.animation(_:)` attaches the animation to writes made through that binding, so the toggle's own mutation runs in an animated transaction and every view it changes animates.

withAnimation is a stage manager calling 'slow motion' for one cue, and every performer that moves on that cue moves slowly; animation(_:value:) is a standing instruction to one performer to move slowly whenever one particular prop changes, whoever changed it.

  • withAnimation only animates the view whose button action called it.
  • animation(_:value:) animates every change to the view, whatever value changed.
  • Where animation(_:value:) sits in the modifier chain makes no difference.
  • The value-less animation(_:) is the modern preferred form of implicit animation.
  • Code that runs after an await inside withAnimation's Task still animates.