暂停和恢复AnimateWithDuration动画ios

时间:2016-10-03 18:50:40

标签: ios objective-c animation uilabel

所以我在animateWithDuration方法的帮助下完成了动画,但我需要在应用程序进入后台时停止动画,并在应用程序返回前景时恢复。

有没有办法实现这个目标: -

我的动画:标签在一定间隔后淡出淡出;

- (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 个答案:

答案 0 :(得分: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. 请勿忘记取消注册UIApplication中的两个viewDidDisappear:通知。