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 UIImageViews 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.