当调用UIDynamicAnimator时,如何停止applicationDidEnterBackground?另外,如何在applicationWillEnterForeground中启动计时器?下面是我的代码:
在我的游戏浏览器.m中
-(void)stoptime
{
[_animator removeAllBehaviors]; //_animator is my UIDynamicAnimator
[time invalidate];
}在我的应用程序中委托.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
gameViewController *new=[[gameViewController alloc]initWithNibName:@"gameViewController" bundle:nil];
[new stoptime];
}发布于 2014-08-26 04:36:21
使用NSNotificationCenter。在视图控制器中,侦听通知(在viewDidLoad中):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stoptime) name:@"StopTimeNotification" object:nil];然后,在你的applicationDidEnterBackground:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"StopTimeNotification" object:nil];
}最后,在stoptime方法中:
-(void)stoptime {
[_animator removeAllBehaviors]; //_animator is my UIDynamicAnimator
[time invalidate];
time = nil;
}当您的视图控制器即将被释放时,一定要调用以下内容:
[[NSNotificationCenter defaultCenter] removeObserver:self];另外两项评论:
当使计时器失效时,最好也将它设置为nil,以便以后可以重用它
2-不要使用new作为任何变量的名称,它是一个保留关键字。
编辑
您可以使用类似的方法重新启动您的计时器。
https://stackoverflow.com/questions/25498065
复制相似问题