gpt4 book ai didi

ios - 暂停和恢复 AnimateWithDuration 动画 ios

转载 作者:行者123 更新时间:2023-11-29 00:39:36 35 4
gpt4 key购买 nike

所以我已经在方法 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];
}];
}];
}];
}];
}];
}];
}];
}];
}

最佳答案

由于您的动画看起来是由多个较小的子动画无限重复组成的序列,因此您可以随时跟踪您在动画循环中的位置,在您的应用变为非事件状态时停止动画,然后重新启动动画当您的应用再次激活时。

[编辑:下面添加了示例代码和解释。]

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 通知。

关于ios - 暂停和恢复 AnimateWithDuration 动画 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838448/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com