Ali AD
Ali AD

Reputation: 13

Animate image scale change while preserving its width and height

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")

Upvotes: 0

Views: 206

Answers (1)

HangarRash
HangarRash

Reputation: 15005

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.

Upvotes: 1

Related Questions