swiftcore-animationcalayeruiviewanimation

swift - chaining UIView animation


I'd like to animate my UIImageView with these steps:

  1. scale 2 times
  2. scaled image movement

So I'm trying like this:

UIView.animate(withDuration: 0.5, animations: {
   self.imageView.transform = CGAffineTransform(scaleX: 2, y: 2)                        
}) { _ in                        
   UIView.animate(withDuration: 1.0, animations: {
        self.imageView.transform = CGAffineTransform(translationX: -50, y: -50)
                        
   }) { _ in

        //other steps
   }
 }

But the result is that before moving imageView got back to previous size and then moving. How to move scaled view?

also I've tried to change layer's anchorPoint inside the animate block with no success

And maybe there is more simple way to achieve my goal: ability to zoom out image, then focus at 3 custom points, then zoom in to the initial size.


Solution

  • For your first question: Since you are assigning the transform to a new one without keeping the old configuration, so in your second animation, your view will transform to the default scale (1, 1). All you need is just assigning the transform to a new one which contains all the old configurations plus the new you want.

    UIView.animate(withDuration: 0.5, animations: {
      self.button.transform = self.button.transform.scaledBy(x: 2, y: 2)
    }) { _ in
      UIView.animate(withDuration: 1.0, animations: {
        self.button.transform = self.button.transform.translatedBy(x: -50, y: -50)
      }) { _ in
        
      }
    }
    

    It's even better if you use animateKeyframes