UIView animateWithDuration不尊重延迟

时间:2013-08-07 11:56:14

标签: iphone ios objective-c uikit animatewithduration

我想安排一系列动画,动画在屏幕上显示文字。完成后的最后一个动画应该触发应该运行游戏的游戏刻度逻辑。

出于某种原因,一切都在发生。任何人都可以解释为什么会发生这种情况或更好的选择吗?

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

1 个答案:

答案 0 :(得分:3)

我的建议是将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秒的“错觉”。