iOS 中的嵌套动画:在 animateWithDuration 中组合动画和非动画代码?

Nested animation in iOS: combine animated and non-animated code in animateWithDuration?

Nested animation

简而言之,嵌套动画允许同时 运行 2 个独立动画。但是如果我有一个包含动画和非动画代码的复杂代码怎么办?我指的是以下情况:

[UIView animateWithDuration:...
animations:^{

    ...//code1 - with animation
    ...//code2 - also contains code which shouldn't be animated
}];

Code2 看起来像下面方法的调用:

- (void)someMethod {

    ...//code3 - without animation
    [self someMethod2Animated:NO completion:^{

        [self someMethod2Animated:YES completion:^{

            NSLog(...);
        }];
    }];
}

其中 someMethod2 执行相同的代码,但可能在 animatedWithDuration:animations:completion: 内部,具体取决于给定的 BOOL 变量。

如果我 运行 这段代码,那么我会看到所有代码部分都是动画的。我试图将 code2 的持续时间设置为零,但它没有效果(代码仍然是动画的)。

你能建议如何在不重写所有代码的情况下解决这个问题吗?我听说过一些像 ReactiveCocoa 这样的解决方案,但它们似乎只适用于 network/separate 异步请求。

已编辑

PromiseKit 2.0 允许将封闭的动画块转换为块序列,但它不支持 iOS 7 我需要。

UIView animateWithDuration 非常适合非常简单(非交互式)的动画。对于您正在做的事情,您应该深入研究 CAAnimation。 UIView animateWithDuration 将为块内任何位置发生的任何视图更改设置动画。

您基本上是想有条件地链接您的动画。这是关于链接动画的一个很好的答案。 How to chain different CAAnimation in an iOS application

我找到的最佳解决方案是 RZViewActions。我的案例的优势:

  • 像处理没有嵌套块的对象一样处理动画;

  • 串行和并行动画是 "sequences" 和 "groups",它们也是从简单的动画数组创建的对象;

  • 就拿它的demo,去掉[self.spinner startAnimating];,运行然后多次点击屏幕。即使没有任何特殊代码,很多动画也会同时执行(如果animateWithDuration:下一个动画会杀死前一个未完成的动画)。

第二个解决方案可能是 PromiseKit 2.0。它可能在 iOS 7 下 运行 但它需要一些魔法才能添加到项目中。