How to crossfade between 2 images on iPhone using Core Animation
You were almost there.
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 5.0;
crossFade.fromValue = image1.CGImage;
crossFade.toValue = image2.CGImage;
[self.imageView.layer addAnimation:crossFade forKey:@"animateContents"];
Note that the animation is independent of the actual values/contents of the UIImageView. Therefore you'll need to
self.imageView.image = image2;
... to set the final result for your image.
I found this somewhere - it works great.
Swift
UIView.transition(with: imageView, duration: 0.33, options: .transitionCrossDissolve, animations: {
imageView.image = image
}, completion: nil)
Objc
UIImage * toImage = [UIImage imageNamed:@"image.png"];
[UIView transitionWithView:self.view
duration:0.33f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
self.imageview.image = toImage;
} completion:NULL];
extension UIImageView
{
func mySetImage(image:UIImage)
{
guard let currentImage = self.image where currentImage != image else { return }
let animation = CATransition()
animation.duration = 0.3
animation.type = kCATransitionFade
layer.add(animation, forKey: "ImageFade")
self.image = image
}
}