Implicit .animation() vs explicit withAnimation
How SwiftUI ties animation to state changes.
.animation(value:) animates a view when a tracked value changes; withAnimation wraps the state mutation so all dependent views animate.
WHAT THIS TESTS This checks whether you understand that SwiftUI animates state transitions, and the two ways to declare which transitions animate: attached to a view, or attached to the mutation.
A GOOD ANSWER COVERS The implicit modifier .animation(_:value:) is placed on a view and watches a specific value. When that value changes, SwiftUI animates the affected properties of that view and its subtree with the given animation. It is local and declarative. The explicit form, withAnimation(_:) { }, wraps the code that mutates state. Any view whose body depends on the state changed inside the closure animates using that animation. It is imperative at the call site and coordinates multiple views at once.
COMMON WRONG ANSWERS Mentioning the old .animation(_) modifier without a value, which is deprecated because it animated based on any change and caused surprising behavior. Saying withAnimation only affects the view it is written in; it actually affects all views observing that state. Claiming you cannot combine them.
LIKELY FOLLOW-UPS What happens if both are present: an explicit withAnimation can override or compose with an implicit one. How to disable animation for a particular change: wrap it in withAnimation(nil) or withTransaction. What Animatable and animatableData have to do with custom curves.
ONE CONCRETE EXAMPLE Implicit: a heart icon that scales when liked. You write Image(systemName:).scaleEffect(isLiked ? 1.3 : 1).animation(.spring(), value: isLiked). Toggling isLiked animates just that icon. Explicit: tapping a button should slide in a detail panel and fade a background dim view together. You write withAnimation(.easeInOut) { showDetail = true }, and both the panel offset and the overlay opacity, which read showDetail, animate in sync. The explicit form ties the coordinated transition to the single user action.
Interview question
You wrap showPanel = true inside withAnimation, and three different views read showPanel. What animates?
- a.Only views that also have an explicit .animation modifier attached
- b.Only the view containing the button that called withAnimation
- c.Nothing, because withAnimation needs a value parameter like .animation does
- d.All three views, since each depends on the state changed in the closureCorrect
Why? this is the answer
withAnimation animates every view whose body depends on state mutated inside its closure. It is not limited to the call site, and unlike .animation(value:) it takes no value argument.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles