Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyway to animate UIImageView's contentMode from aspectFill to aspectFit?

[UIView animateWithDuration:30.0f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
    v.contentMode = UIViewContentModeScaleAspectFit;
    v.frame = v.superview.bounds;
} completion:^(BOOL finished) {
    [v removeFromSuperview];
}];

I want a UIImageView to resize and change the contentMode, but the code above doesn't work. It changed the contentMode immediately at the beginning of the animation.

Is there any way to animate it?

like image 660
adali Avatar asked Apr 03 '13 02:04

adali


4 Answers

As others have mentioned there is no way to animate contentMode.

This library exists to solve that though! https://github.com/patrickbdev/PBImageView

imageView.contentMode = .scaleAspectFit

UIView.animateWithDuration(1) {
    self.imageView.contentMode = .scaleAspectFill
}
like image 78
Patrick Avatar answered Nov 07 '22 05:11

Patrick


Based on "What Can Be Animated", you cannot animate contentmode directly. However, you can try to animate the frame in order to get the same behavior as contentmode does. Once animation completed, you can set contentmode and frame back to original.

like image 20
Jingwei Avatar answered Nov 07 '22 04:11

Jingwei


UIImageView inherits its contentMode property from UIView. It is not one of the properties you can animate. More information can be found in the View Programming Guide.

like image 2
MishieMoo Avatar answered Nov 07 '22 04:11

MishieMoo


To achieve that, you can use this component on github : https://github.com/VivienCormier/UIImageViewModeScaleAspect

like image 2
Rémy Virin Avatar answered Nov 07 '22 04:11

Rémy Virin