UIImageView Fade-In and Fade-Out (pulsating) Animation

Today just a small code snippet to create a nice fade-in-fade-out (or call it pulsating) effect on a given UIImage. To accomplish this within a UIView you have to do only a few steps:

  • Create a UIImageView containing your image.
  • Create the animation with its properties.
  • Set the animation for the specific layer containing your image.

In words of code you will have to do something like the following:

//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"];

Thats all folks and have fun,

Andreas

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

0 Responses to “UIImageView Fade-In and Fade-Out (pulsating) Animation”


  • No Comments

Leave a Reply