繁体   English   中英

如何在applicationDidEnterBackground中停止UIDy​​namicAnimator

[英]How to stop UIDynamicAnimator in applicationDidEnterBackground

我怎样才能停止UIDynamicAnimatorapplicationDidEnterBackground叫? 另外,如何在applicationWillEnterForeground启动计时器? 这是我的代码:

在我的游戏viewcontroller.m中

 -(void)stoptime
{
     [_animator removeAllBehaviors]; //_animator is my UIDynamicAnimator
      [time invalidate];
}

在我的应用程序委托中

- (void)applicationDidEnterBackground:(UIApplication *)application
{
       gameViewController *new=[[gameViewController   alloc]initWithNibName:@"gameViewController" bundle:nil];
    [new stoptime];
}

使用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];

另外两条评论:

1-使计时器无效时,最好将其设置为nil ,以便以后再使用

2-不要将new用作任何变量的名称,这是一个保留关键字。

编辑

您可以使用类似的方法重新启动计时器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM