带有延迟和持续时间Ios的动画标签 [英] Animating Labels with delay and duration Ios
问题描述
因此,我尝试对标签进行动画处理,该标签将从文本开始,然后在一定时间后消失,新文本出现在屏幕上,屏幕将在一定时间后消失,然后出现新文本。我希望这可以递归发生。我需要继续设置动画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动画,然后在
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.
第二,Is是获得这种动画的正确方法。还是有更好的方法来做到这一点?
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的动画标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!