5

私のアプリには、bezierPathに沿ってアニメーション化したCALayerの配列があります。アプリを閉じて再度開くと、レイヤーがアニメーション化されず、アプリを閉じる前と同じ位置にありません。アプリ内の2つのボタンでトリガーすると機能するpauseLayerとresumeLayerの2つのメソッドを実装しましたが、アプリを閉じた後は機能しません。コードは次のとおりです

   - (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;
        }


}
4

4 に答える 4

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

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

  }

上記でやろうとしていることの問題は、画面に表示されていたものではない、View Controller のまったく新しいインスタンスを作成していることです。pauseLayersそのため、メッセージを送信しても何も起こりません。

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

次のコードを実装のどこかに追加する必要がありますmosquitosViewController(通常は viewDidLoad で行います)。

// 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]];
于 2011-07-27T18:11:06.863 に答える
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.
 */

}

于 2011-07-24T10:01:14.917 に答える
0

マルチタスク後にアニメーションを再開する方法の詳細については、この投稿に対する私の回答を参照してください。

アプリがバックグラウンドから再開したときに中断したところからアニメーションを復元する

于 2011-11-02T16:24:14.973 に答える