logo
Login Register

Sponsored

iOS Fade in and Fade out View Effects

Sprite Fading Effects

Building an iOS app without using any 2D framework like Cocos2D for graphics/animation implies that if you need to roll out  your code to achieve any simple animation effects. The good news is that with some simple code & basic understanding of the Animation and Sprites, these effects that can be achieved relatively easy without having to use those framework if you really don't have to, thus keeping you project lean and avoid external dependencies.

Here we will quickly see if we can achieve a simple Fade In and Fade out Effect. This can be used on any type of UIView (UIImageView , UIButton etc.). Fade in means that view appears in main view slowly and Fade out means the opposite.

The most important thing is alpha(transparency) property of any view along with the basic beginAnimation method of the UIView together will be used to achieve the effect.

Demonstration

The following video shows the FadeIn /Fade Out effects in the simulator.

Code Snippet

The following code snippet shows the main methods.

  1. -(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration   andWait:(NSTimeInterval)wait
  2. {
  3.         [UIView beginAnimations: @"Fade Out" context:nil];
  4.        
  5.         // wait for time before begin
  6.         [UIView setAnimationDelay:wait];
  7.        
  8.         // druation of animation
  9.         [UIView setAnimationDuration:duration];
  10.         viewToDissolve.alpha = 0.0;
  11.         [UIView commitAnimations];
  12. }
  13.  
  14. -(void)fadeIn:(UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration         andWait:(NSTimeInterval)wait
  15. {
  16.         [UIView beginAnimations: @"Fade In" context:nil];
  17.        
  18.         // wait for time before begin
  19.         [UIView setAnimationDelay:wait];
  20.  
  21.                 // druation of animation
  22.         [UIView setAnimationDuration:duration];
  23.         viewToFadeIn.alpha = 1;
  24.         [UIView commitAnimations];
  25.        
  26. }
  27.  
  28. /**
  29.         Fade in from fade out
  30.  */
  31. -(void) fadeInFromFadeOut: (UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration
  32. {
  33.         viewToFadeIn.hidden=NO;
  34.         [self fadeOut:viewToFadeIn withDuration:1 andWait:0];
  35.         [self fadeIn:viewToFadeIn withDuration:duration andWait:0];
  36.        
  37. }
  38.  
  39. -(void) buttonClicked :(id)sender
  40. {
  41.         NSLog(@"Button clicked");
  42.        
  43.         // Each button is given a tag
  44.         int tag = ((UIButton*)sender).tag;
  45.         if (tag ==1)
  46.         {
  47.  
  48.                 sprite.alpha  =1;
  49.                 [self fadeOut : sprite withDuration: 3 andWait : 1 ];
  50.         }
  51.         else if (tag ==2)
  52.         {
  53.                 sprite.alpha  =0;
  54.                 [self fadeIn : sprite withDuration: 3 andWait : 1 ];
  55.         }
  56.         else
  57.         {
  58.  
  59.                 [self fadeInFromFadeOut:sprite withDuration:4];        
  60.         }
  61.  
  62.  
  63. }

The project source code can be downloaded using the attached link.

AttachmentSize
ViewFadeIn.zip39 KB
Socialize & Share :
Software Quote
"Your job is being a professor and researcher: That's one hell of a good excuse for some of the brain-damages of minix. "
-(Linus Torvalds to Andrew Tanenbaum)
Anonymous's picture
 #

Awesome -- you saved me a lot of hunting around. Thanks man.

 

Subscribe & Follow

RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!
Feed via Email:

Sponsored Links

Sponsored Apps

Job Change Alert for Linkedin


Contact2CRM for Salesforce


Advertise on DeveloperFeed

About

Manpreet is an Architect & Software developer currently focusing on developing mobile apps on the iOS (iPhone/iPad) Platform. He’s the founder of a small iPhone development studio called Livrona.

Subscribe to Developer Feed iPhone Blog

Tags

Who's online

There are currently 0 users and 2 guests online.