🎉 Black Friday Sale! Save 30% when you subscribe today.

SwiftUI Animation: Composable Architecture

Episode #136 • Feb 22, 2021 • Subscriber-Only

The Composable Architecture mostly “just works” with SwiftUI animations out of the box, except for one key situation: animations driven by asynchronous effects. To fix this we are led to a really surprising transformation of Combine schedulers.

Collection
SwiftUI Animations
SwiftUI Animation: Composable Architecture
Locked

Unlock This Episode

Our Free plan includes 1 subscriber-only episode of your choice, plus weekly updates from our newsletter.

Sign in with GitHub

Introduction

This is starting to show the power behind explicit animations. They allow you to be far more targeted in what you want to animate and how you want to animate.

It’s also worth noting that SwiftUI used to more heavily lean on implicit animations. For example, it used to be that if you ever made a change to the data source powering a List view then those changes would automatically animate. That would cause rows to animate into place or slide away when removed. That made for a really nice demo since you could get animation basically for free, but also meant that a lot of really strong opinions were hardcoded directly in SwiftUI’s foundational components, which seems strange. However, in iOS 14 and Xcode 12 that behavior was changed so that you had to start using explicit animations in order to animate a List view. So it appears that SwiftUI is heading more towards favoring explicit animations over implicit animations.

So, that’s the basics of SwiftUI animations. They come in two major flavors: implicit and explicit. Implicit is heavily state driven, in that whenever state changes it will automatically animate all changes to the view based on that state change. You don’t have the ability to perform animations when an event occurs, it only happens when state changes. This is why it was so hard to prevent animations when the reset button was tapped. Because that is an event and implicit animations don’t handle events well.

On the other hand, explicit animations are far more targeted and more event driven. This means that when events occur we can tell SwiftUI to animate a state change. This is why it was so easy to opt in or out of animation when the animation button was tapped.

Synchronous animation


References

  • isowords
    Point-Free

    A word game by us, written in the Composable Architecture.

  • Collection: Schedulers
    Brandon Williams & Stephen Celis • Jun 4, 2020

    We previously did a deep-dive into all things Combine schedulers. We showed what they are used for, how to use them in generic contexts, and how to write tests that make the passage of time controllable and determinstic.

    Note

    There’s a lot of great material in the community covering almost every aspect of the Combine framework, but sadly Combine’s Scheduler protocol hasn’t gotten much attention. It’s a pretty mysterious protocol, and Apple does not provide much documentation about it, but it is incredibly powerful and can allow one to test how time flows through complex publishers.

  • combine-schedulers
    Brandon Williams & Stephen Celis • Jun 14, 2020

    An open source library that provides schedulers for making Combine more testable and more versatile.

Downloads

Get started with our free plan

Our free plan includes 1 subscriber-only episode of your choice, access to 64 free episodes with transcripts and code samples, and weekly updates from our newsletter.

View plans and pricing