2016-10-03 14 views
0

アニメーションは、メソッドanimateWithDurationの助けを借りて行っていますが、アプリがバックグラウンドになり、アプリがフォアグラウンドに戻ったときにアニメーションを停止する必要があります。AnimateWithDurationアニメーションの一時停止と再開ios

私はこれを達成することができる方法があります: -

マイアニメーション:一定の間隔後のフェードアウトのラベルフェードは、

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [self MyLabelAnimation]; 

} 

- (void)MyLabelAnimation { 
    self.myLabel.text = @"Text 1"; 
    [UIView animateWithDuration:0.3 animations:^{ 
     self.myLabel.alpha = 1.0; 
    } completion:^(BOOL finished) { 

     [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
      self.myLabel.alpha = 0.0; 
     } completion:^(BOOL finished) { 

      self.myLabel.text = @"Text 2"; 
      [UIView animateWithDuration:0.3 animations:^{ 
       self.myLabel.alpha = 1.0; 
      } completion:^(BOOL finished) { 
       [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
        self.myLabel.alpha = 0.0; 
       } completion:^(BOOL finished) { 

        self.myLabel.text = @"Text 3"; 
        [UIView animateWithDuration:0.3 animations:^{ 
         self.myLabel.alpha = 1.0; 
        } completion:^(BOOL finished) { 

         [UIView animateWithDuration:0.3 delay:2.7 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
          self.myLabel.alpha = 0.0; 
         } completion:^(BOOL finished) { 

          self.myLabel.text = @"Text 4"; 
          [UIView animateWithDuration:0.3 animations:^{ 
           self.myLabel.alpha = 1.0; 
          } completion:^(BOOL finished) { 
           [UIView animateWithDuration:0.0 delay:4.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 
            self.myLabel.alpha = 0.0; 
           } completion:^(BOOL finished) { 

            [self MyLabelAnimation]; 
           }]; 
          }]; 
         }]; 
        }]; 
       }]; 
      }]; 
     }]; 
    }]; 
} 

答えて

1

アニメーションが無限に繰り返さいくつかの小さなサブアニメーションのシーケンスであるように思われるので、アプリが非アクティブになったときに、アニメーションを停止し、すべての回であなたはアニメーションループのどこにいるかを追跡することができ、あなたのアプリが再びアクティブになったらアニメーションを再開してください。

[編集:サンプルコードと説明は下記を追加。]

ViewController.mで:

@interface ViewController() 
{ 
    int numStages; // 4 * 3 = 12 in our example (4 different text labels, 
    // with 3 substages (alpha ramping up, alpha constant, and alpha ramping down) for each) 
    int globalStage; // varies from 0 to numStages - 1. 0 initially 
    bool animationIsActive; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    numStages = 4 * 3; // 4 * 3 = 12 in our example (4 different text labels, 
    // with 3 substages for each text label's appearance (alpha ramping up, alpha constant, and alpha ramping down)) 
    globalStage = 0; // varies from 0 to numStages - 1. 0 initially 
    animationIsActive = NO; 

    self.myLabel.alpha = 0.0; 
} 

- (void)animateStage:(int)stage { 
    NSLog(@"animateStage called with argument stage = %d", stage); 

    // make a note in our instance variable of where we need to restart 
    // the animation THE NEXT TIME if it is interrupted or paused 
    // during the current animation: 
    globalStage = (stage + 1) % numStages; 

    self.myLabel.text = [NSString stringWithFormat:@"Text %d", (stage/3) + 1]; 

    switch (stage % 3) { 
    case 0: // ramp up alpha from 0 to 1 
    { 
     [UIView animateWithDuration:0.3 animations:^{ 
     self.myLabel.alpha = 1.0; 
     } completion:^(BOOL finished) { 
     // only proceed to next stage if the animation is supposed to be active: 
     if (animationIsActive) { 
      [self animateStage:globalStage]; 
     } 
     }]; 
    } 
     break; 
    case 1: // keep alpha constant at 1 (see comment below) 
    { 
     [UIView animateWithDuration:2.7 animations:^{ 
     self.myLabel.alpha = 0.99999; // changing the 0.99999 to 1.0 causes 
     // this stage to be short-circuited. probably because iOS realizes 
     // that alpha is not really changing in this stage and, being too clever 
     // by half, decides to skip this stage altogether. but 0.99999 is 
     // as close to 1.0 as makes no difference. 
     } completion:^(BOOL finished) { 
     // only proceed to next stage if the animation is supposed to be active: 
     if (animationIsActive) { 
      [self animateStage:globalStage]; 
     } 
     }]; 
    } 
     break; 
    case 2: // ramp down alpha from 1 to 0 
    { 
     [UIView animateWithDuration:0.3 animations:^{ 
     self.myLabel.alpha = 0.0; 
     } completion:^(BOOL finished) { 
     // only proceed to next stage if the animation is supposed to be active: 
     if (animationIsActive) { 
      [self animateStage:globalStage]; 
     } 
     }]; 
    } 
     break; 
    default: 
     break; 
    } 

} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    NSLog(@"viewWillAppear: called"); 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(didBecomeActive:) 
    name:UIApplicationDidBecomeActiveNotification 
    object:[UIApplication sharedApplication]]; 
    [[NSNotificationCenter defaultCenter] 
    addObserver:self 
    selector:@selector(willResignActive:) 
    name:UIApplicationWillResignActiveNotification 
    object:[UIApplication sharedApplication]]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    NSLog(@"viewDidDisappear: called"); 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:UIApplicationDidBecomeActiveNotification 
    object:[UIApplication sharedApplication]]; 
    [[NSNotificationCenter defaultCenter] 
    removeObserver:self 
    name:UIApplicationWillResignActiveNotification 
    object:[UIApplication sharedApplication]]; 
} 

- (void)didBecomeActive:(NSNotification *)notification 
{ 
    NSLog(@"view controller's didBecomeActive: called"); 
    // start the animation is we are stopped 
    if (!animationIsActive) 
    { 
    NSLog(@"animation being (re)started at stage %d", globalStage); 
    [self animateStage:globalStage]; 
    animationIsActive = YES; 
    } 
} 

- (void)willResignActive:(NSNotification *)notification 
{ 
    NSLog(@"view controller's willResignActive: called"); 
    // stop the animation 
    NSLog(@"animation being stopped at stage %d", globalStage); 
    animationIsActive = NO; 
} 

@end 
  1. 私は解体し、4 * 3 = 12にアニメーションを "フラット化" しましたより細かい制御のための段階。
  2. インスタンス変数globalStageanimationIsActiveは、アニメーションのどこにあるのか、アニメーションが実行されているかどうかを追跡するために使用されます。
  3. viewWillAppear:で、アプリがアクティブまたは非アクティブになったときに通知を受けるようにお願いします。これらのイベントが発生すると、私たちのメソッドdidBecomeActive:またはwillResignActive:が呼び出されます。これら2つの方法は、私たちがアニメーションを開始したり停止したりする方法です。
  4. viewDidDisappear:の2つのUIApplication通知の登録を忘れないでください。
+0

どうすればいいですか?私はそれをやめる方法を知らないのですか? –

+0

私は元の答えにいくつかのサンプルコードと詳細な説明を追加しました。楽しい! – inwit

+0

ありがとうございました...それはちょうど私が必要なものです..... –

関連する問題