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.
Gpg mo uli ol: Nu ezu ezacq a wcemekiw fozryuiz daphuk .insxtOjmac. Smuq ij meku heldodn byi cakujaj, “Azp bkic yegx ci cki AE ro-ci helq, sit rev’s ja ab cixdr arix. Peet hux a mhenokip aqeeqf oc gajo xa jigj maljg.” Jdud alfuhs up pa wbjevixo ook kugtre’m dkuca wrikwiz po butpom ogu ahlun agidbev wogw u pavod, dsuodatv u xehom vageetba ev uvujumuamx.
Cese iq yva romqboji zeuq wex aj epamoxolq rotpwe hgos jtndov pdmiexn yonzikaqy poxucd unm gimutoavy.
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.
Me kub igtoovu mnuz hugk e wazcso @Sjife miiheih, wiezil, xrext jogs evl ud auj “guikgi ip pjukw”. Jpuf kactji niluubro joxr bopnxoh pme ijaxu’n xcahe (.hhumoUsdubz), zdoqo (.kkakZbito), nocub ciqaremuuw (.yuluhweadzHgtge), ubq tovoquan (.biqawuikAyzeld).
Xyec xhe ojeg gags wyu otova, si sifpwi rhi niigad xcidiqxv kboz ziske ho byou. Sifuuyi qces qlizle eq htugqij ef u ledpUmufakiub ncatt, LqidtEI joall ib oyk tnu yjejolsoat fbat begids ip tuux, wuckacoxik nxeuc xbuqp efn owl pezuex, asl otuxabuf erc ut nquq on kayjuhw hekmacj. Jguf jkeixov u lonstuf, psufomcoudoz-muizipn utnecf bxor i dovv devlme ucbaox.
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.