如何“可选”基于块的UIView动画?

| 我的自定义控件具有-setValue:animated:方法,该方法带有
animated
标志。 在iOS 4之前,我会这样写动画:
if (animated) {
    [UIView beginAnimations:@\"Foo\"];
    [UIView setAnimationDuration:5.0];
}
// ... layout views ...
if (animated) {
    [UIView commitAnimations];
}
现在我写了这个:
[UIView animateWithDuration:(animated ? 5.0 : 0.0) animations:^{
    // ... layout views ...
}];
但是:这导致某些元素无法动画显示! 我多次调用此方法(第一次不使用动画,第二次使用动画),因此第二次取消动画,将新帧设置为“硬”(不使用动画)。 如何使用块方法实现可选动画?     
已邀请:
您可以定义要在块中进行的所有更改。然后,如果要使更改动起来,则可以将块馈入
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();
}
    

要回复问题请先登录注册