title: Consecutively animate array of UIViews with Swift
body: <p>I have an array of views (<code>UIStackView.arrangedSubviews</code>) which I want to animate with <code>UIView.animate()</code>. But I only want to start animating the next element, when the previous one is finished.</p>
  
  <p>I want to find a somewhat "elegant" solution to this. I have tried two different approaches, but I'm stuck on both of them. How can I achieve this?</p>
  
  <h2>First attempt: Recursive closure</h2>
  
  <pre><code>let animate = { (views: [UIView]) -&gt; () in
      guard let view = views.first else { return }
      UIView.animate(
          withDuration: 0.5,
          animations: {
              view.frame.origin.y -= 30.0
              view.alpha = 1.0
      },
          completion: { finished in
              if finished {
                  // TODO: Only start animating next view when previous is finished?
                  animate(views.removeFirst()) // ERROR: Variable used within itself
              }
      })
  }
  </code></pre>
  
  <h2>Second attempt: Using array map</h2>
  
  <pre><code>let _ = stackView.arrangedSubviews.map { view -&gt; UIView in
      view.frame.origin.y += 30.0
      UIView.animate(
          withDuration: 0.5,
          animations: {
              view.frame.origin.y -= 30.0
              view.alpha = 1.0
          },
          completion: { finished in
              // TODO: Only start animating next view when previous is finished?
      })
  
      return view
  }
  </code></pre>