我想安排一系列动画,以动画形式在屏幕上介绍文字。最后一个动画在完成后应触发应该运行游戏的游戏滴答逻辑。

由于某种原因,所有事情都会立即发生。谁能阐明为什么会这样或有更好的选择?

[self.view bringSubviewToFront:_ViewIntroSeconds];

[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^(void) {
                     [_IntroLabel1 setAlpha:1];
                 }
                 completion:^(BOOL finished){
                 }];

[UIView animateWithDuration:0.4 delay:3.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^(void) {
                     [_IntroLabel2 setAlpha:1];
                     [_IntroLabel1 setAlpha:0];
                     [_IntroLabel1 setCenter:CGPointMake(0, _IntroLabel1.center.y)];
                 }
                 completion:^(BOOL finished){
                 }];

[UIView animateWithDuration:0.4 delay:5.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^(void) {
                     [_IntroLabel3 setAlpha:1];
                     [_IntroLabel2 setAlpha:0];
                     [_IntroLabel2 setCenter:CGPointMake(0, _IntroLabel2.center.y)];
                 }
                 completion:^(BOOL finished){
                     [self updateGame];
                     [self updateClock];

                     [_ViewIntroSeconds setAlpha:0];
                 }];

最佳答案

我的建议是将delay设置为0,并将后续的UIView动画块放置在先前completion语句的animateWithDuration块中:

[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
                 animations:^(void) {
                     [_IntroLabel1 setAlpha:1];
                 }
                 completion:^(BOOL finished){
                     [UIView animateWithDuration:0.4 delay:2.6 options:UIViewAnimationOptionCurveEaseInOut
                                      animations:^(void) {
                                          [_IntroLabel2 setAlpha:1];
                                          [_IntroLabel1 setAlpha:0];
                                          [_IntroLabel1 setCenter:CGPointMake(0, _IntroLabel1.center.y)];
                                      }
                                      completion:^(BOOL finished){
                                          [UIView animateWithDuration:0.4 delay:1.6 options:UIViewAnimationOptionCurveEaseInOut
                                                           animations:^(void) {
                                                               [_IntroLabel3 setAlpha:1];
                                                               [_IntroLabel2 setAlpha:0];
                                                               [_IntroLabel2 setCenter:CGPointMake(0, _IntroLabel2.center.y)];
                                                           }
                                                           completion:^(BOOL finished){
                                                               [self updateGame];
                                                               [self updateClock];

                                                               [_ViewIntroSeconds setAlpha:0];
                                                           }];
                                      }];
                 }];

这将给您所需的效果。

编辑:发生这种情况的原因是,UIView animateWithDuration块开始动画,并继续执行以下代码,而动画仍然有效。因此,建议在完成块中执行“下一个”动画效果。否则,几乎所有animateWithDuration请求都将立即执行。

编辑2:我在块中编辑了delay,分别为您提供了0、3和5秒的“幻觉”。

关于iphone - UIView animateWithDuration不考虑延迟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18102747/

10-11 04:07