簡體   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