title: Wait for Swift animation to complete before executing code
body: <p>I am trying to animate a UIImageView and then hide the image view after animation is complete. However the imageview gets hidden before the animation is completed. I looked at similar questions and they recommend implementing an animation listener or executing the .hidden code within the animation code upon completion however I'm not sure how to affect this within the shakeView() function below.</p>
  
  <p>How do I show the shake animation and hide the image view only after the animation is complete?</p>
  
  <p>Animation is called using the following code:</p>
  
  <pre><code>shakeView(image1!)
  shakeView(image2)
  image1!.hidden = true
  image2.hidden = true
  </code></pre>
  
  <p>The animation function itself looks like this:</p>
  
  <pre><code>func shakeView(iv: UIImageView){
      var shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
      shake.duration = 0.1
      shake.repeatCount = 2
      shake.autoreverses = true
  
      var from_point:CGPoint = CGPointMake(iv.center.x - 5, iv.center.y)
      var from_value:NSValue = NSValue(CGPoint: from_point)
  
      var to_point:CGPoint = CGPointMake(iv.center.x + 5, iv.center.y)
      var to_value:NSValue = NSValue(CGPoint: to_point)
  
      shake.fromValue = from_value
      shake.toValue = to_value
      iv.layer.addAnimation(shake, forKey: "position")
  }
  </code></pre>