Home > database >  Animate image scale change while preserving its width and height
Animate image scale change while preserving its width and height

Time:01-07

I want to animate zooming-in and out from image in Swift using CAAnimation while preserving the original width and height of the image (i.e. show smaller area in the image enlarged).

I tried to use the following animation and it did animate the change in scale properly, but also changed the width and height of my image accordingly:

let animation = CABasicAnimation(keyPath: "transform.scale")
animation.fromValue = 1.4
animation.toValue = 1.0
animation.duration = 2

self.imageView.layer.add(animation, forKey: "scaleAnimation")

CodePudding user response:

One solution would be to put the image view in a container view with the container view's clipToBounds set to true. This way as the image view is scaled larger, it will be clipped by its container view and appear to remain the same size. Make sure the container view's frame matches the unzoomed size of the image view.

  • Related