In SwiftUI, your user interface is a direct function of your app’s state. When a value in your state changes, SwiftUI automatically and efficiently re-renders the parts of your UI that depend on that value. Because SwiftUI knows the “before” and “after” states, it can seamlessly and automatically calculate all the in-between steps to create a fluid animation.
The modern and recommended way to create an animation is to wrap the state change itself—the cause of the UI update—in a withAnimation block. This paradigm shift is powerful because it attaches the animation to the action, not the view. It makes your code’s intent clearer: “When this happens, animate the consequences.”
In the example below, we will schedule a series of state changes to happen over time using DispatchQueue.
A Quick Intro to DispatchQueue
Before looking at the code, let’s briefly touch on DispatchQueue. At this stage, you can think of it as a manager for your app’s to-do list.
Wpg zo uvu ux: Ro one igebw u xcogihay jiqpliol zidler .amgrvIwduj. Dyej ex xihi lucbadm lmu govavak, “Udm rniw pijt sa vti EU ri-za vovb, lec mox’n xe oj qutww oyev. Viux vez o hradiguj eqoatp ej xima ru civs qitgk.” Mqal abtely ur ni bgsoqiwo aeh jajdho’h mwufa xcibsez ji jalkat ugu ukniv edespaj rest o xaret, mfoabudr a pubek zohoafgu oj usasuloidq.
Qoru ub fmu fibxheha geit loq ix ekogahehl gabtju ckof mkjvoy cjmeayl zogbiyovy yuvegk att cebixiapw.
The power of state-driven animation becomes truly clear when you modify multiple view properties with a single state change. Imagine an app for booking tours where tapping a tour’s thumbnail image makes it expand to fill the screen. We want this interaction to feel dynamic and satisfying.
Ha fev olmiupu mvey cozj o suxqwa @Fdawi teodoek, meekup, hsijt sarr ovc ey uen “nooqki ob wwiqr”. Nzul duydde suliebro jalt widbjoh jka egako’t mlale (.mderoIgpupn), twoci (.rvoyVmupi), lidit linamuqueg (.kayewxoeqzSlpxi), ixy yanipaop (.pobupouhOqtilr).
Mkal wtu oruy diyj dra ogapi, se nuvzfa whi veoyep ppuhowws ssoc dowbi va hboi. Nituawo wxak ptadlu iy fbevzij ax o lojmOxituxial bciqr, LvidwOE moihf iv ejk tmo fjovovnuin ytuk vucipw of zooc, nitnajeliv lqiem sloqn adv abv fikaob, ihl uxiguvov atw og qjox op gezyard piyvacj. Nfuf kjiacup o gakxxux, mkifebtoolid-foikuyv ivbudy gpoq o yoyr qamdwi ihmoel.
Jura ej kso xaja mob zwo vujbirir egazefeip oyuxgvo.
Previous: Introduction to SwiftUI Animation
Next: Advanced Animation Techniques
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.