0

I'm trying to write code where when a trigger happens an instance of UIImageView creates a slow "growing" effect with a CGAffineTransform running for 20 seconds.

The issue happens when the trigger happens again before the first transform has completed. Instead of the image resetting back to it's original size, it shrinks exponentially, depending on when the trigger happened during the first transformation.

Here is my current code:

func changeCategoryImage() {
    self.categoryPanoramaImageView.transform = CGAffineTransform.identity

    UIView.transition(with: self.categoryPanoramaImageView, duration: 0.4, options: .transitionCrossDissolve, animations: {
        self.categoryPanoramaImageView.image = self.newPanoramaImage
    }) { (done) in
        UIView.animate(withDuration: 10, animations: {
            self.categoryPanoramaImageView.transform = CGAffineTransform(scaleX: 5.0, y: 5.0)
        })

    }
}

I was under the impression that CGAffineTransform.identity would reset the image back to it's original size. This is seems to not be the case.

How can I halt and reset the current running transformation in order to start a new one?

Alex Ritter
  • 1,009
  • 3
  • 18
  • 40
  • Probably a duplicate of https://stackoverflow.com/questions/554997/cancel-a-uiview-animation – matt Jan 24 '18 at 00:20

1 Answers1

2

I was under the impression that CGAffineTransform.identity would reset the image back to it's original size. This is seems to not be the case.

Correct. It isn't the case. That's not how animations work. You are making a direct change to the underlying view (i.e. the model layer), but the animation is still present (i.e. the presentation layer).

Thus, you are setting the view's transform to the identity, but you are not removing the existing animation. And animations are additive by default. Hence the result you are observing.

Try inserting

self.categoryPanoramaImageView.layer.removeAllAnimations() 

as the first line of your function. That way, you will remove the animation and start over from the identity.

matt
  • 515,959
  • 87
  • 875
  • 1,141