Explicit Animations with withAnimation
Wrap state mutations to control timing.
Explicit Animations with withAnimation is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
When You Want Control
Sometimes you want to decide exactly which change animates. withAnimation wraps a state change so its results tween.
The withAnimation Block
You put your state mutation inside withAnimation. Every view update caused by that change animates together.
withAnimation {
isExpanded.toggle()
}Explicit vs Implicit
Implicit animation lives on the view; explicit animation lives at the change site. You choose what animates by where you mutate.
Add a Curve
Pass a curve to withAnimation just like the modifier. The animation argument shapes the timing of the whole block.
withAnimation(.easeInOut(duration: 0.3)) {
show.toggle()
}Trigger from a Button
A common pattern is wrapping a toggle in a Button action so a tap animates the resulting layout change.
Button("Toggle") {
withAnimation { open.toggle() }
}Many Views, One Block
If one state change affects several views, withAnimation animates them all in sync from a single wrapped mutation.
Use a Spring Feel
Springs give natural, bouncy motion. Pass .spring to withAnimation for UI that feels physical and responsive.
withAnimation(.spring) {
selected = id
}Animate Conditional Views
Toggling a boolean that adds or removes a view inside withAnimation animates its appearance, not just its properties.
withAnimation {
showDetail = true
}Return a Value
withAnimation can return the value its closure produces, so you can animate and read a result in one expression.
Completion Handling
Newer APIs let you run code after the motion finishes via a completion callback on withAnimation, great for chaining steps.
Keep Blocks Focused
Only put the change you want to animate inside the block. Unrelated work outside withAnimation stays instant and clean.
Quick Check
What is the key difference between the two animation styles?
Recap: withAnimation
You saw how withAnimation wraps a state change so every resulting update tweens together. It gives precise control over what animates. 👍
Frequently asked questions
Is the “Explicit Animations with withAnimation” lesson free?
Yes — the full text of “Explicit Animations with withAnimation” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Explicit Animations with withAnimation”?
Wrap state mutations to control timing. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Explicit Animations with withAnimation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Implicit Animations
- Explicit Animations with withAnimation
- Insertion & Removal Transitions
- Spring & Custom Timing Curves