I\'m doing this to learn how to work with Core Animation animatable properties on iPhone (not to learn how to crossfade images, per se).
Reading similar questions on
My suggestion would be to just use two UIImageView
s on top of each other. The one on top has the image1, and on bottom is image2:
- (void) crossfade {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
imageView1.alpha = !imageView1.alpha;
[UIView commitAnimations];
}
That will fade between the two images whenever you call it. If you want to just do a single fade and remove the first image after you're done:
- (void) crossfade {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.5];
[UIView setAnimationDelegate:imageView1];
[UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
imageView1.alpha = 0
[UIView commitAnimations];
}
Edit:
If you're looking for a reason why your code doesn't work, it's because you're just setting the property to the new image, then adding a useless animation. CABasicAnimation
has the fromValue
and toValue
that need to be set, then add that animation to the layer, and it should do the animation. Don't set the contents manually.
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.
Solution in swift
let image1:UIImage = UIImage(named: "someImage1")!;
let image2:UIImage = UIImage(named: "someImage2")!;
let crossFade:CABasicAnimation = CABasicAnimation(keyPath: "contents");
crossFade.duration = 5.0;
crossFade.fromValue = image1.CGImage;
crossFade.toValue = image2.CGImage;
imageView.layer.addAnimation(crossFade, forKey:"animateContents");
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBarHidden=false;
UIImage *img=[UIImage imageNamed:@"images.jpeg"];
imgview=[[UIImageView alloc]init];
imgview.frame=CGRectMake(30,90, img.size.width, img.size.height);
[self.view addSubview:imgview];
[self animateImages];
}
- (void)animateImages
{
static int count = 0;
NSArray *animationImages = @[[UIImage imageNamed:@"images.jpeg"], [UIImage imageNamed:@"images (1).jpeg"]];
UIImage *image = [animationImages objectAtIndex:(count % [animationImages count])];
[UIView transitionWithView:imgview
duration:2.0f // animation duration
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imgview.image = image;
} completion:^(BOOL finished) {
[self animateImages];
count++;
}];
}
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
}
}