booboo-a-choo
booboo-a-choo

Reputation: 779

iphone splash screen multi-tasking

I have added a fading splash screen to an iPhone with the code below

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

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

This is for an old app that I am now enabling multi-tasking for. I have an issue where if the app is closed (via the home button or locked) I want to cancel the animation. I have added the following code to remove the splash view when the app is closed

-(void) applicationDidEnterBackground:(UIApplication *)application
   [splashView removeFromSuperview];
   [splashView release];
}

The app crashes if the app is closed before the splash screen animation completes as the splash screen in removed in applicationDidEnterBackground, so when startupAnimationDone gets called (after applicationDidEnterBackground) there is nothing to remove so it crashes.

Is there a way to cancel the animation in applicationDidEnterBackground?

Upvotes: 0

Views: 972

Answers (2)

Stephen Darlington
Stephen Darlington

Reputation: 52565

You need to force your animation to finish before you remove the view. You can do that by creating a new animation to your end point. Set a very short duration and make sure you use the +setAnimationBeginsFromCurrentState: method to start from the current state

The longer answer can be found in my answer to an old question.

Upvotes: 1

xhan
xhan

Reputation: 6287

call splashView = nil after [splashView release]

or call [splashView.layer removeAllAnimations]

Upvotes: 0

Related Questions