
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.
- -(void)fadeOut:(UIView*)viewToDissolve withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait
- {
- [UIView beginAnimations: @"Fade Out" context:nil];
- // wait for time before begin
- [UIView setAnimationDelay:wait];
- // druation of animation
- [UIView setAnimationDuration:duration];
- viewToDissolve.alpha = 0.0;
- [UIView commitAnimations];
- }
- -(void)fadeIn:(UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration andWait:(NSTimeInterval)wait
- {
- [UIView beginAnimations: @"Fade In" context:nil];
- // wait for time before begin
- [UIView setAnimationDelay:wait];
- // druation of animation
- [UIView setAnimationDuration:duration];
- viewToFadeIn.alpha = 1;
- [UIView commitAnimations];
- }
- /**
- Fade in from fade out
- */
- -(void) fadeInFromFadeOut: (UIView*)viewToFadeIn withDuration:(NSTimeInterval)duration
- {
- viewToFadeIn.hidden=NO;
- [self fadeOut:viewToFadeIn withDuration:1 andWait:0];
- [self fadeIn:viewToFadeIn withDuration:duration andWait:0];
- }
- -(void) buttonClicked :(id)sender
- {
- NSLog(@"Button clicked");
- // Each button is given a tag
- int tag = ((UIButton*)sender).tag;
- if (tag ==1)
- {
- sprite.alpha =1;
- [self fadeOut : sprite withDuration: 3 andWait : 1 ];
- }
- else if (tag ==2)
- {
- sprite.alpha =0;
- [self fadeIn : sprite withDuration: 3 andWait : 1 ];
- }
- else
- {
- [self fadeInFromFadeOut:sprite withDuration:4];
- }
- }
The project source code can be downloaded using the attached link.
| Attachment | Size |
|---|---|
| ViewFadeIn.zip | 39 KB |
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) |
Related Links
- iOS Fade in and Fade out View Effects
- Copy PDF Files from Main Bundle to Document Folder in iOS
- Using NSError Explained
- How to find Image link from an RSS Item in iOS?
- How to remove HTML tags from a String in iOS?
- Essential Keyboard Shortcuts for XCode 4?
- How to parse Query String into KeyValue Pair?
- How to send email from iPhone/iPad?
- Using NSUserDefaults to store Settings in iOS
- App crashes on launching Mail Composer View in iPad
- mvohra's blog
- Add new comment
- 3549 reads




Software Quote


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
Awesome -- you saved me a lot of hunting around. Thanks man.