【问题标题】:Wait for Animation to finish in iOS? [closed]等待动画在 iOS 中完成? [关闭]
【发布时间】:2013-05-03 21:44:17
【问题描述】:

我想在动画完成后执行 doSomethingElse。另一个限制是动画代码可以有不同的持续时间。我怎样才能做到这一点?谢谢!

-(void) doAnimationThenSomethingElse {
  [self doAnimation];
  [self doSomethingElse];
}

这个例子不起作用:

animationDuration = 1;
[UIView animateWithDuration:animationDuration
    animations:^{
      [self doAnimation];
    } completion:^(BOOL finished) {
      [self doSomethingElse];
    }
];

【问题讨论】:

  • 您提供的内容无法解决您的问题。请参阅下面的答案,要获得您想要的行为,您必须能够看到执行动画的代码(此处由 doAnimation 封装)。

标签: ios objective-c animation


【解决方案1】:

当你不是动画的作者时,可以通过事务完成块在动画结束时获得回调:

[CATransaction setCompletionBlock:^{
     // doSomethingElse
}];
// doSomething

【讨论】:

    【解决方案2】:

    使用方块动画:

    [UIView animateWithDuration:animationDuration
        animations:^{
            // Put animation code here
        } completion:^(BOOL finished) {
            // Put here the code that you want to execute when the animation finishes
        }
    ];
    

    【讨论】:

    • 我没有具体的时长,见上
    • @SebastianOberste-Vorth 我不明白为什么会有问题。你可以传入任何你想要的持续时间。
    • 我不知道之前的持续时间,这取决于doAnimation函数,这是一种随机
    • @SebastianOberste-Vorth 也许更多上下文和实际代码会有所帮助,我仍然可以想象这种方法正在被使用。
    • animationDuration 应该放什么?假设 doAnimation 采用 5-10 秒差异的随机动画...
    【解决方案3】:

    您需要能够访问正在运行的动画的特定实例,以便协调每个动画的完成操作。在您的示例中,[self doAnimation] 不会向我们展示任何动画,因此您提供的内容无法“解决”您的问题。

    实现您想要的效果的方法很少,但这取决于您要处理的动画类型。

    正如其他答案中所指出的,在视图上的动画之后执行代码的最常见方法是在:animateWithDuration:completion: 中传递一个完成块:处理属性更改动画的另一种方法是在其中设置一个 CATransaction 完成块交易范围。

    但是,这些特定方法基本上是用于对视图的属性或层次结构更改进行动画处理。当您的动画涉及视图及其属性时,这是推荐的方式,但它并不涵盖您可能在 iOS 中找到的所有类型的动画。根据您的问题,尚不清楚您正在使用哪种动画(或如何使用或为什么使用),但如果您实际上正在触摸 CAAnimations 的实例(关键帧动画或一组动画),您将通常做的是设置一个委托:

    CAAnimation *animation = [CAAnimation animation];
    [animation setDelegate:self];
    [animatedLayer addAnimation:animation forKeyPath:nil];
    
    // Then implement the delegate method on your class
    - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
    {
        // Do post-animation work here.
    }
    

    关键是,完成处理的实现方式取决于动画的实现方式。在这种情况下我们看不到后者,所以我们无法确定前者。

    【讨论】:

      【解决方案4】:

      http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html

      有 UIView 文档,向下滚动到

      使用块动画视图

      动画视图

      查看将您跳转到页面不同部分的超链接,这些部分解释了如何为视图设置动画,您想要的位于下方 “用块动画视图” 阅读方法的名称会使它们不言自明

      【讨论】:

      • 他们都有animateWithDuration属性,在代码执行之前我不知道
      【解决方案5】:

      我会从你的 cmets 推荐你:

      -(void) doAnimation{
          [self setAnimationDelegate:self];
          [self setAnimationDidStopSelector:@selector(finishAnimation:finished:context:)];
          [self doAnimation];
      }
      
      - (void)finishAnimation:(NSString *)animationId finished:(BOOL)finished context:(void *)context {
          [self doSomethingElse];
      }
      

      【讨论】:

      • setAnimationDidStopSelector: 如果不在 UIView 动画块中调用,则不执行任何操作。
      • 你是对的,更新了代码
      猜你喜欢
      • 2013-02-13
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 2019-01-01
      相关资源
      最近更新 更多