Wednesday, November 19, 2008

Fading Default.png when iPhone app starts

More iPhone code. I wanted to have the startup logo (Default.png) graphic fade into the first screen after the program starts. This is a much nicer transition than just letting the view switch instantly.


1) Add the new image view to the class.

UIImageView *splashView;

And add this selector:
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context;


2) at the very bottom of "applicationDidFinishLaunching" add:

splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:@"Default.png"];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
[UIView commitAnimations];



3) Add this selector to cleanup the image and save memory.

- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[splashView removeFromSuperview];
[splashView release];
}



4) Awesome bonus bit. Add this below the splashView.alpha = 0.0 line. It adds that extra bit of coolness to the transition.

splashView.frame = CGRectMake(-60, -60, 440, 600);

7 comments:

Aaron said...

Is there a way to do this with the image rotated 90 degrees to the right, for an app starting up in landscape?

PK said...

friggin awesome !!

Leo said...

Pretty Cool, all right! :) Thanks!

Tim Harrison said...

Thanks, you help me out loads. :)

Michael said...

I did not know it is that easy :) Thanks!!

joelm said...

Thanks, excellent implementation!

epidote said...

Thank you! Very nice.