问题来源:
前面在使用跑马灯的时候(参考:http://blog.csdn.net/ipromiseu/article/details/7412111),一旦程序进入到后台后,再次打开程序,发现动画已经中断了,只有其它新的动画出现,那上一个动画就会被自动停止,动画是无法自动恢复的,比如tab切换等有动画的操作就会导致一个页面的动画停止,可以在页面中加入setNeedsDisplay来重启动画,也可以自己给他添加一个恢复的方法。其实这种方法也不仅仅用于动画的恢复,还有很多其它一旦进入后台就会被中断的处理都可以使用此方法恢复,
(转载请保留此文字:本文来源:[ios开发技巧之]动画Animation中断后的恢复 http://blog.csdn.net/ipromiseu/article/details/7436718] ,write by Gray.Luo,接受ios/android app 外包开发: [email protected] )
方法如下:
1.在Delegate.m中从后台恢复的代理中加入一个处理,我这里,直接在这里加一个消息发送,一旦APP从后台恢复,则会调用 applicationDidBecomeActive,我在此函数中向动画处理的控制器发送一个恢复的消息,让其恢复。
- (void)applicationDidBecomeActive:(UIApplication *)application { /* Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. */ NSLog(@"applicationDidBecomeActive"); NSDictionary *dictionary = [NSDictionary dictionaryWithObject:RESUME_ANIMATIONS forKey:RESUME_ANIMATIONS ]; [[NSNotificationCenter defaultCenter] postNotificationName:RESUME_ANIMATIONS object:nil userInfo:dictionary]; }
首先,监听此消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(subEventHandle:) name:RESUME_ANIMATIONS object:nil];
- (void)subEventHandle:(NSNotification *)note{ NSString *recvCmdString = [[note userInfo] objectForKey:RESUME_ANIMATIONS]; if ([recvCmdString isEqualToString:RESUME_ANIMATIONS]) { [aTitleLabel removeFromSuperview]; [self lampTextText:nsTitleString]; } }