暂停和恢复AnimateWithDuration动画ios [英] Pause and resume AnimateWithDuration animations ios

查看:2646
本文介绍了暂停和恢复AnimateWithDuration动画ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have done animations based with the help of method animateWithDuration but i need to stop animation when app goes in background and resume when app comes back in foreground.

有没有办法可以做到这一点: -

Is there a way i can achieve this:-

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

My animations : label fade in fade out after certain intervals;

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


推荐答案

因为你的动画似乎是无限重复的几个较小的子动画序列,您可以随时跟踪动画循环中的位置,在应用变为非活动状态时停止动画,并在应用再次变为活动状态时重新启动动画。

Since your animation appears to be a sequence of several smaller sub-animations repeated ad infinitum, you could keep track of where you are in the animation loop at all times, stop the animation when your app becomes inactive, and restart the animation when your app becomes active again.

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. 实例变量 globalStage animationIsActive 用于跟踪我们在动画中的位置以及动画是否正在运行。

  3. In viewWillAppear:我们要求在应用变为有效或无效时收到通知。发生这些事件时,我们的方法 didBecomeActive: willResignActive:被调用。这两种方法是我们(重新)启动和停止动画的地方。

  4. 不要忘记取消注册两个 UIApplication 通知在 viewDidDisappear:

  1. I've broken up and "flattened" your animation into 4 * 3 = 12 stages for finer control.
  2. The instance variables globalStage and animationIsActive are used to keep track of where we are in the animation and whether or not the animation is running.
  3. In viewWillAppear: we ask to be notified when the app becomes active or inactive. When these events occur, our methods didBecomeActive: or willResignActive: are called. These two methods are where we (re)start and stop our animations.
  4. Don't forget to unregister for the two UIApplication notifications in viewDidDisappear:.

这篇关于暂停和恢复AnimateWithDuration动画ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆