How to animate the change of image in an UIImageView? How to animate the change of image in an UIImageView? ios ios

How to animate the change of image in an UIImageView?


[UIView transitionWithView:textFieldimageView                  duration:0.2f                   options:UIViewAnimationOptionTransitionCrossDissolve                animations:^{                    imageView.image = newImage;                } completion:nil];

is another possibility


I am not sure if you can animate UIViews with fade effect as it seems all supported view transitions are defined in UIViewAnimationTransition enumeration. Fading effect can be achieved using CoreAnimation. Sample example for this approach:

#import <QuartzCore/QuartzCore.h>...imageView.image = [UIImage imageNamed:(i % 2) ? @"3.jpg" : @"4.jpg"];CATransition *transition = [CATransition animation];transition.duration = 1.0f;transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];transition.type = kCATransitionFade;[imageView.layer addAnimation:transition forKey:nil];


In the words of Michael Scott, keep it simple stupid. Here is a simple way to do this in Swift 3 and Swift 4:

UIView.transition(with: imageView,                  duration: 0.75,                  options: .transitionCrossDissolve,                  animations: { self.imageView.image = toImage },                  completion: nil)