How to "optionalize" block-based UIView animations?
My custom control has a method -setValue:animated:, which takes an animated
flag.
Before iOS 4, I would have written the animation thus:
if (animated) {
[UIView beginAnimations:@"Foo"];
[UIView setAnimationDuration:5.0];
}
// ... layout views ...
if (animated) {
[UIView commitAnimations];
}
Now I wrote this:
[UIView animateWithDuration:(animated ? 5.0 : 0.0) animations:^{
// ... layout views ...
}];
BUT: this leads to some elements not animating!
I call this method more than just once (first time without, second time with animation), so the second time ro开发者_如何学运维und the animation is cancelled, setting my new frame "hard" (without animation).
How to implement optional animation with the blocks approach?
You can define all of the changes you want to make in a block. You then either feed the block to UIView animate...
if you want the changes animated, or execute it directly to make the changes without animation.
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();
}
精彩评论