Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS fade-in/fade-out animation
//Get the image and create the image view
//You also can use an image view created with Interface Builder
UIImage *image = [UIImage imageNamed:@"yourfavouriteimage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//Add the image view to your main view
//(This is optional if it was created with Interface Builder)
...
//Create an animation with pulsating effect
CABasicAnimation *theAnimation;
//within the animation we will adjust the "opacity"
//value of the layer
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
//animation lasts 0.4 seconds
theAnimation.duration=0.4;
//and it repeats forever
theAnimation.repeatCount= 1e100f;
//we want a reverse animation
theAnimation.autoreverses=YES;
//justify the opacity as you like (1=fully visible, 0=unvisible)
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.1];
//Assign the animation to your UIImage layer and the
//animation will start immediately
[imageView.layer addAnimation:theAnimation forKey:@"animateOpacity"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment