2016-10-03 537 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:被調用。這兩種方法是我們(重新)開始和停止我們的動畫的地方。
  4. 不要忘記在viewDidDisappear:註銷兩個UIApplication通知。
+0

我該怎麼做..我不知道如何阻止它? –

+0

我已經添加了一些示例代碼和詳細的解釋到我的原始答案。請享用! – inwit

+0

謝謝你......那正是我需要的..... –