【问题标题】:UIView animateWithDuration not respecting delayUIView animateWithDuration 不考虑延迟
【发布时间】:2013-08-08 19:00:18
【问题描述】:

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

出于某种原因,一切都立即发生。谁能解释为什么会发生这种情况或更好的选择?

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

【问题讨论】:

    标签: iphone ios objective-c uikit animatewithduration


    【解决方案1】:

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

    [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 秒的“幻觉”。

    【讨论】:

    • 好主意,但我希望动画持续时间和下一个动画之间的延迟不同
    • 然后在相应的animateWithDuration 块中修改您想要的delay。请注意,delay 将是累积的。如果您在块中将延迟设置为 1、2 和 3 秒,它们将在以下时间被调用:1、3 和 6 秒。
    • @n00bProgrammer 嗨,我正在尝试实现同样的事情,我确实在完成块中使用了后续动画,但它仍然不符合延迟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多