[英]How to stop UIDynamicAnimator in applicationDidEnterBackground
How can I stop a UIDynamicAnimator
when applicationDidEnterBackground
is called? 我怎样才能停止
UIDynamicAnimator
时applicationDidEnterBackground
叫? Also, how can I start the timer in applicationWillEnterForeground
? 另外,如何在
applicationWillEnterForeground
启动计时器? Here's the code I have: 这是我的代码:
in my game viewcontroller.m 在我的游戏viewcontroller.m中
-(void)stoptime
{
[_animator removeAllBehaviors]; //_animator is my UIDynamicAnimator
[time invalidate];
}
in my app delegate.m 在我的应用程序委托中
- (void)applicationDidEnterBackground:(UIApplication *)application
{
gameViewController *new=[[gameViewController alloc]initWithNibName:@"gameViewController" bundle:nil];
[new stoptime];
}
Use NSNotificationCenter
. 使用
NSNotificationCenter
。 In your view controller, listen for notifications (in viewDidLoad
): 在视图控制器中,侦听通知(在
viewDidLoad
):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stoptime) name:@"StopTimeNotification" object:nil];
Then, in your applicationDidEnterBackground:
然后,在您的
applicationDidEnterBackground:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"StopTimeNotification" object:nil];
}
Finally, in your stoptime
method: 最后,在您的
stoptime
方法中:
-(void)stoptime {
[_animator removeAllBehaviors]; //_animator is my UIDynamicAnimator
[time invalidate];
time = nil;
}
Be sure to call the following when your view controller is about to be freed: 确保要释放视图控制器时,请调用以下命令:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Two additional comments: 另外两条评论:
1 - When invalidating a timer, it's good practice to set it to nil
as well so you can reuse it later 1-使计时器无效时,最好将其设置为
nil
,以便以后再使用
2 - Don't use new
as a name for any variable, it's a reserved keyword. 2-不要将
new
用作任何变量的名称,这是一个保留关键字。
EDIT 编辑
You can use a similar method for restarting your timer. 您可以使用类似的方法重新启动计时器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.