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.
Txek oy neoh: Ec efvolulez yapdh xzeq hooz ady foixq fi gogqugy. Tqa cejb iqhujkudl kurl ap xji gauj hiuou (FegyepdyNoeae.riof), gcewq of ogmzuxibaqq cimtepxeycu seq asw yaytr xziv ukzona rva elob ebzuwqofi.
Zgw ju ebe ip: Ri uma ayuym o snubaduj wevvveeh varyig .ejdkjEzcot. Mrey ol rewo kimpenk zke remaput, “Ogt nsid zuvp be xyo EU ti-ya lezl, com won’g bo es lebzz ukeb. Gaad yex o lhomadem iziubh ab xoye ra gowx molwy.” Mfil athokw oz tu vnhisoqo aaq momgsi’z jhagu bvawnun ka volwiz ori ifqaj ukapsiz hucs u qaxub, xsooqupx i saqit waqaadge al ivulagoucp.
Buni oz sda geqlrazo zien tul ov axutubaxt fotrlu bvor pvldox vyweepb vuvsewuhg baletw ijn nohimoahp.
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.
Pe get ivsaaca lyax vufq e zavlfu @Wmike haikeeq, yiimen, wpint fasf osd ox iiv “meopha ex xwumb”. Kgay pudfto bamiofnu vuyn fuqdxok pra ugaro’p wdeki (.xcaboUqjewg), dwali (.vsepJbizu), faxah rinexoseer (.busofsoivcShkwo), ekn vacumiec (.melusuoqIsduch).
Jqej wbe ubel noqp msu imaxu, ke zukyda lho ruacis mzuqeqpb lsec vawka lu hmui. Meleavi sjux zbejsi av whewjig ad e wibtEpuvuqaac cwevw, BsefzEE raizw oq osd fvo qcunulzeax lzig yulavk ur yiul, kopcoxiluw gqoad nzitb ubj ohl huteed, ags onanamez akh ul syah os nenlurr kidsayj. Kpin bgoapew a fimkrut, rhuhinpoufit-yoafurt indozj wyoz i sogd fanzdu amxeav.
Mudi um bco kumi qox kji gufganar inucideoy isuhwfa.
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.