问题描述
所以我试图为标签设置动画,该标签将以文本开头,然后在一定时间后会消失,新文本出现在屏幕上,在一定时间后会消失,然后新文本出现.我希望这递归地发生.我需要继续动画 15 秒,接下来的 15 秒再次发生同样的事情.
So i was trying to animate label which will start with a text then after certain time it will fade and new text comes to the screen which will fade after certain time and then new text comes. I want this to happen recursively. I need to continue animating for 15 seconds , the again same thing happens for next 15 seconds.
- (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 个动画的完成块的末尾调用了该方法.
So Here i am calling mylabel animation from viewdidload and then i called the method at the end of the completion block of 4th animation.
这里一切都按预期工作,但最后一个动画到第一个动画过渡不平滑,因为其他过渡.是不是因为我错过了什么.我真的不明白为什么会这样.
Here everything is working as expected but the last animation to first animation transition is not smooth as other transition. Is it because i missed something . I really am not able to figure out why is it happening.
其次,是获得这样的动画的正确方法.或者有没有更好的方法来做到这一点?
Secondly, Is is the right way to get the animation like this. Or is there a better way to do this ?
推荐答案
你可能会发现这样的事情更容易管理:
You might find something like this a bit easier to manage:
- (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];
}];
}];
}
这篇关于带有延迟和持续时间 Ios 的动画标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!