animation标签延迟和持续时间Ios

所以我试图animation的标签,将开始与一个文本,然后一定的时间后,它会淡入淡出,新的文本来到屏幕,将在一定的时间后淡出,然后新的文本来。 我希望这个发生recursion。 我需要继续animation15秒,同样的事情发生在接下来的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调用mylabelanimation,然后在第四个animation完成块的末尾调用方法。

这里一切都按预期工作,但是最后一个animation到第一个animation转换并不像其他转换那样平滑 。 是因为我错过了什么 我真的无法弄清楚为什么会这样。

其次,是这样得到animation的正确方法。 还是有更好的方法来做到这一点?

你可能会发现这样更容易pipe理:

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

试试这个代码….

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