5

私のアプリには、私がbezierPathに沿ってアニメーション化したCALayerの配列があります。私がアプリケーションを閉じて再オープンすると、レイヤーはアニメーション化されず、アプリケーションを閉じる前と同じ位置に表示されません。私は2つのメソッド、pauseLayerとresumeLayerを実装しています。これは、アプリケーション内の2つのボタンでトリガーするときに機能しますが、アプリケーションを閉じた後は機能しません。コードは以下のマルチタスクから復帰した後にCAAnimationを再開する方法

- (void)pauseLayers{ 

    for(int y=0; y<=end;y++) 
    { 



     CFTimeInterval pausedTime = [car[y] convertTime:CACurrentMediaTime() fromLayer:nil]; 
     car[y].speed = 0.0; 
     car[y].timeOffset = pausedTime; 

     standardUserDefaults[y] = [NSUserDefaults standardUserDefaults]; 


     if (standardUserDefaults[y]) { 
      [standardUserDefaults[y] setDouble:pausedTime forKey:@"pausedTime"]; 
      [standardUserDefaults[y] synchronize]; 
     } 


     NSLog(@"saving positions"); 


     } 


} 

-(void)resumeLayers 

{ 




    for(int y=0; y<=end;y++) 
    { 




     standardUserDefaults[y] = [NSUserDefaults standardUserDefaults];  
     car[y].timeOffset = [standardUserDefaults[y] doubleForKey:@"pausedTime"]; 

    CFTimeInterval pausedTime = [car[y] timeOffset]; 
    car[y].speed = 1.0; 
    car[y].timeOffset = 0.0; 
    car[y].beginTime = 0.0; 

    CFTimeInterval timeSincePause = [car[y] convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; 
    car[y].beginTime = timeSincePause; 
     } 


} 
+0

アプリがバックグラウンドに入ったときにNSLogが呼び出されていますか? – MiguelB

+0

はい。それは奇妙なことです:( –

+0

私は、pauseLayersとresumeLayersを呼び出すアプリケーションデリゲートメソッドを表示します。 – MiguelB

答えて

0
- (void)applicationDidEnterBackground:(UIApplication *)application 

{

NSLog(@"1"); 
mosquitosViewController *mvc = [[mosquitosViewController alloc] init]; 
[mvc pauseLayers]; 

/* 
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
*/ 

}

3
- (void)applicationDidEnterBackground:(UIApplication *)application { 

mosquitosViewController *mvc = [[mosquitosViewController alloc] init]; 
    [mvc pauseLayers]; 

    } 

であるあなたが上記のやろうとしているものの問題は、あなたが完全に新しいインスタンスを作成しているということです画面上に表示されていたビューコントローラではありません。だからこそ、pauseLayersメッセージを送信すると何も起こりません。

あなたがする必要があることは、あなたのアプリがバックグラウンドに出入りするときの通知を受け取るために登録し、その通知が到着したときに適切なメソッド(pauseLayersおよびresumeLayers)を呼び出すことです。

あなたは(私は通常のviewDidLoadでそう)どこかmosquitosViewController実装に次のコードを追加する必要があります

// Register for notification that app did enter background 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(pauseLayers) 
             name:UIApplicationDidEnterBackgroundNotification 
             object:[UIApplication sharedApplication]]; 

// Register for notification that app did enter foreground 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(resumeLayers) 
             name:UIApplicationWillEnterForegroundNotification 
             object:[UIApplication sharedApplication]]; 
関連する問題