如何"自定义"基于块的UIView动画?

Yan*_*yer 2 uiview uiviewanimation ios4 objective-c-blocks

我的自定义控件有一个方法-setValue:animated:,它带有一个animated标志.

在iOS 4之前,我会编写动画:

if (animated) {
    [UIView beginAnimations:@"Foo"];
    [UIView setAnimationDuration:5.0];
}
// ... layout views ...
if (animated) {
    [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)

现在我写了这个:

[UIView animateWithDuration:(animated ? 5.0 : 0.0) animations:^{
    // ... layout views ...
}];
Run Code Online (Sandbox Code Playgroud)

但是:这导致一些元素没有动画!

我称这个方法不止一次(第一次没有,第二次用动画),所以第二次动画被取消,将我的新帧设置为"硬"(没有动画).

如何使用块方法实现可选动画?

odr*_*drm 9

您可以定义要在块中进行的所有更改.然后,UIView animate...如果要对更改进行动画处理,则可以将块提供给,或者直接执行它以在没有动画的情况下进行更改.

void (^myViewChanges)(void) = ^() {
    myView.alpha = 0.5;
    // other changes you want to make to animatable properties
};

if (animated) {
    [UIView animateWithDuration:5.0f animations:myViewChanges];
} else {
    myViewChanges();
}
Run Code Online (Sandbox Code Playgroud)