带有延迟和持续时间 Ios 的动画标签
Posted
技术标签:
【中文标题】带有延迟和持续时间 Ios 的动画标签【英文标题】:Animating Labels with delay and duration Ios 【发布时间】:2016-10-03 06:33:13 【问题描述】:所以我试图为标签设置动画,该标签将以文本开头,然后在一定时间后它会消失,新文本出现在屏幕上,在一定时间后会消失,然后出现新文本。我希望这递归地发生。我需要继续动画 15 秒,接下来的 15 秒会发生同样的事情。
- (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];
];
];
];
];
];
];
];
];
所以我在这里从 viewdidload 调用 mylabel 动画,然后在第 4 个动画的完成块末尾调用该方法。
这里一切都按预期工作,但最后一个动画到第一个动画过渡不平滑,因为其他过渡。是不是因为我错过了什么。我真的无法弄清楚为什么会这样。
其次,Is 是获得这样的动画的正确方法。或者有更好的方法吗?
【问题讨论】:
我也有同样的问题。 你们有谁知道如何在后台暂停动画并在前台再次恢复 我在这里得到了答案link 【参考方案1】:您可能会发现这样的事情更容易管理:
- (void)viewDidLoad
[super viewDidLoad];
self.counter = 0;
self.animations = @[
@@"Text":@"Hello", @"Duration":@(2.7),
@@"Text":@"Text 1", @"Duration":@(2.7),
@@"Text":@"Text 2", @"Duration":@(2.7),
@@"Text":@"Text 3", @"Duration":@(2.7),
@@"Text":@"Text 4", @"Duration":@(4.8),
];
[self animate:0];
- (void)animate:(int)counter
self.counter = counter % self.animations.count;
NSDictionary *item = self.animations[self.counter];
self.myLabel.alpha = 0;
self.myLabel.text = item[@"Text"];
[UIView animateWithDuration:0.3
animations:^
self.myLabel.alpha = 1.0;
completion:^(BOOL finished)
[UIView animateWithDuration:0.3
delay:[item[@"Duration"] floatValue]
options:UIViewAnimationOptionCurveEaseInOut
animations:^
self.myLabel.alpha = 0.0;
completion:^(BOOL finished)
[self animate:++self.counter];
];
];
【讨论】:
你知道当应用程序进入后台时如何暂停动画吗? 将全局BOOL
添加到名为shouldStopAnimation
的类中。检查animate
函数开头的值。如果已设置,则立即返回。为 ApplicationWillEnterBackground 通知添加一个观察者,并在收到时将 bool 值设置为YES
。您还可以通过将动画设置为 no NO
并重新调用 animate:self.counter
来重新启动动画。
如果答案解决了您的问题,请不要忘记将答案标记为正确。【参考方案2】:
试试这个代码....
- (void)viewDidLoad
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
- (void)MyLabelAnimation:(NSTimer*) timer
self->mylabel.text = @"Hello";
[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)
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(MyLabelAnimation:) userInfo:nil repeats:NO];
];
];
];
];
];
];
];
];
【讨论】:
以上是关于带有延迟和持续时间 Ios 的动画标签的主要内容,如果未能解决你的问题,请参考以下文章