title: Chaining animations in SwiftUI
body: <p>I'm working on a relatively complex animation in SwiftUI and am wondering what's the best / most elegant way to chain the various animation phases.</p>
  
  <p>Let's say I have a view that first needs to scale, then wait a few seconds and then fade (and then wait a couple of seconds and start over - indefinitely).</p>
  
  <p>If I try to use several withAnimation() blocks on the same view/stack, they end up interfering with each other and messing up the animation.</p>
  
  <p>The best I could come up with so far, is call a custom function on the initial views .onAppear() modifier and in that function, have withAnimation() blocks for each stage of the animation with delays between them. So, it basically looks something like this:</p>
  
  <pre><code>func doAnimations() {
    withAnimation(...)
  
    DispatchQueue.main.asyncAfter(...)
      withAnimation(...)
  
    DispatchQueue.main.asyncAfter(...)
      withAnimation(...)
  
    ...
  
  }
  </code></pre>
  
  <p>It ends up being pretty long and not very "pretty". I'm sure there has to be a better/nicer way to do this, but everything I tried so far didn't give me the exact flow I want.</p>
  
  <p>Any ideas/recommendations/tips would be highly appreciated. Thanks!</p>