Code Snippet of the Day: Applying a Basic Animation Fade In, Fade Out to a single Layer of a View. (UIImageView in this Case).
First Declare the UIImageView in your Header File (.h)
#import < UIKit/UIKit.h >
// Remove the Spaces after & before the brackets. Frakkin WordPress.
@interface MyViewController : UIViewController {
UIImageView *didYouKnowBubble;
}
@end
Next you MUST Add the QuartzCore.framework to your Frameworks Group in your Project. If you’re unsure of how to do this, download and open the “View Transitions” Sample iPhone App. You can drag the QuartzCore.framework from that project, into yours.
Next, #import the Framework in your Method File (.m) and then write out your methods to apply the animation.
// MyViewController.m
#import < QuartzCore/QuartzCore.h >
// Remove the Spaces after & before the brackets. Frakkin WordPress.
#define kHideAlpha 0.0
#define kShowAlpha 1.0
#define kAnimationDuration 1.5
@implementation AddViewController
- (void)viewDidLoad {
// Allocate Memory for ImageView & then initialize it with the corresponding Image.
didYouKnowBubble = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"didYouKnow.png"]];
// Set the Frame (Where the Image will Appear), (X Coordinate on the Screen, Y Coordinate on the screen, Width of the Image, Height of the Image);
didYouKnowBubble.frame = CGRectMake(9, 116, 302, 117);
// Hide the Image initially.
didYouKnowBubble.alpha = 0.0;
// Add the Hidden Image to the View.
[self.view addSubview:didYouKnowBubble];
// Create and Apply a Timer so that the Image will wait 1 Second before beginning to Appear.
NSTimer *showBubbleTimer = [[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(showBubble:) userInfo:nil repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:showBubbleTimer forMode:NSDefaultRunLoopMode];
[showBubbleTimer release];
}
- (void)showBubble:(NSTimer *)timer {
// Set the Ending Alpha to 1.0 (Fully Opaque).
didYouKnowBubble.alpha = kShowAlpha;
// Begin the Animation Block.
CABasicAnimation *fadeInAnimation;
fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
// Set the Duration of the Animation (Pre-defined above.)
fadeInAnimation.duration = kAnimationDuration;
// Set the Beginning Alpha of the Animation (Pre-defined above.)
fadeInAnimation.fromValue = [NSNumber numberWithFloat:kHideAlpha];
// Set the Ending Alpha of the Animation (Pre-defined above.)
fadeInAnimation.toValue = [NSNumber numberWithFloat:kShowAlpha];
[fadeInAnimation setDelegate: self];
// Apply the Animation to the UIImageView.
[[didYouKnowBubble layer] addAnimation:fadeInAnimation forKey:@"animateOpacity"];
// Create and Apply a Timer so that the Image will wait 15 Seconds before beginning to Disappear.
NSTimer *hideBubbleTimer = [[NSTimer timerWithTimeInterval:15.0 target:self selector:@selector(hideBubble:) userInfo:nil repeats:NO] retain];
[[NSRunLoop currentRunLoop] addTimer:hideBubbleTimer forMode:NSDefaultRunLoopMode];
[hideBubbleTimer release];
}
- (void)hideBubble:(NSTimer *)timer {
// Begin the Animation Block.
CABasicAnimation *fadeOutAnimation;
fadeOutAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
// Set the Duration of the Animation (Pre-defined above.)
fadeOutAnimation.duration = kAnimationDuration;
// Set the Beginning Alpha of the Animation (Pre-defined above.)
fadeOutAnimation.fromValue = [NSNumber numberWithFloat:kShowAlpha];
// Set the Ending Alpha of the Animation (Pre-defined above.)
fadeOutAnimation.toValue = [NSNumber numberWithFloat:kHideAlpha];
[fadeOutAnimation setDelegate: self];
// Apply the Animation to the UIImageView.
[[didYouKnowBubble layer] addAnimation:fadeOutAnimation forKey:@"animateOpacity"];
// Set the Ending Alpha to 0.0 (Completely Hidden).
didYouKnowBubble.alpha = kHideAlpha;
}
Done.
Make sure you deallocate the memory for your object when you release the controller.
- (void)dealloc {
NSLog(@"MyViewController Dealloc Called.");
[didYouKnowBubble release];
[super dealloc];
}