Anyway to animate UIImageView's contentMode from aspectFill to aspectFit?
Asked Answered
S

8

6
[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?

Sholes answered 3/4, 2013 at 2:5 Comment(0)
C
2

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.

Corinecorinna answered 3/4, 2013 at 2:25 Comment(4)
thank you , i realized that it cannot be done by using uiimageview , so i just want find a eazy way to do that :)Sholes
Animating the frame is probably your best bet, then.Corinecorinna
i need to hide it under a view & pre-calc the position to reach the goal :)Sholes
That sounds like a different question to me.Corinecorinna
S
10

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
}
Summersault answered 14/12, 2015 at 10:14 Comment(1)
Worked wonderfully with a complex UIViewController animated transition. Thanks!Donetsk
A
5

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.

Alongshore answered 3/4, 2013 at 2:29 Comment(1)
i think i can only set the imageview as a subview, then calculate the position & size munually, then animate itSholes
C
2

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.

Corinecorinna answered 3/4, 2013 at 2:25 Comment(4)
thank you , i realized that it cannot be done by using uiimageview , so i just want find a eazy way to do that :)Sholes
Animating the frame is probably your best bet, then.Corinecorinna
i need to hide it under a view & pre-calc the position to reach the goal :)Sholes
That sounds like a different question to me.Corinecorinna
S
2

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

Slumber answered 29/1, 2014 at 14:21 Comment(0)
S
1

yes contentMode can NOT be animated but "center" can be - use that for contentMode animation emulation in situations where frame or bounds animation doesn't fit the bill

Semblance answered 21/4, 2014 at 13:49 Comment(0)
P
1

Since iOS 4.0, I think, you can animate the transition with UIView.transition(...) as described in this post. In Swift 5 it looks like this:

let contentMode: UIView.ContentMode = imageView.contentMode == .scaleAspectFit ? .scaleAspectFill : .scaleAspectFit
UIView.transition(with: imageView, duration: 0.5,
                  options: .transitionFlipFromBottom,
                  animations: {
                    self.imageView.contentMode = contentMode
                  },
                  completion: nil)
Philender answered 9/7, 2019 at 16:41 Comment(0)
T
0

MishieMoo is correct. contentMode can NOT be animated. I think you can just change the bounds of the view to achieve what you want.

Tso answered 3/4, 2013 at 2:33 Comment(0)
A
0

I think your need this: https://github.com/vitoziv/VICMAImageView

You can use VICMAImageView like a UIImageView, and animate contentMode in your animation block:

[UIView animateWithDuration:0.3
                 animations:^{
                     imageView.contentMode = UIViewContentModeScaleAspectFill;
                 }];
Acidosis answered 31/12, 2014 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.