I have an animation in viewDidLoad that runs the first time the app is launched. if you exit the app, then launch it again the animation doesn't play.
h开发者_如何学JAVAow would I go about making the animation play each and every time the app is opened,
thanks for any help
In iOS 4, pressing the home button doesn't terminate the app, it suspends it. When the app is made active again, a UIApplicationDidBecomeActiveNotification
is posted. Register for that notification and initiate the animation in your handler.
Edit: Added code below.
Here's one way to do it: Have your view controller become an observer of UIApplicationDidBecomeActiveNotification
in its viewWillAppear:
method.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(performAnimation:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Unregister for the notification in your view controller's viewDidDisappear:
method.
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
Finally, put your animation code in the selector specified when registering to receive the notification.
- (void)performAnimation:(NSNotification *)aNotification {
// Animation code.
}
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
Put the animation in a method like
applicationDidBecomeActive:
of UIApplicationDelegate
Very likely your app isn't quitting and reloading. By default, on iOS 4 apps continue to run when the user 'exits' the app, and continue where they left off when 'restarted'.
Take a look at applicationDidBecomeActive in your app delegate. You could kick off your animation from there when the app is deactivated.
How about set a flag in your application delegate to control this behavior:
Set it to YES
when the app enters the foreground or became active (applicationWillEnterForeground:
, applicationDidBecomeActive:
)
Check if this flag is NO
in -viewWillAppear
in your view controller:
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
if(!delegate.animationPlayed) {
//perform animation here...
delegate.animationPlayed = YES;
}
精彩评论