ryan ryan - 2 months ago 17
iOS Question

iOS - completion block in UIView animateWithDuration gets called too early

I'm trying to do some animation when a table view cell gets selected. For some reason, the completion block is getting called way too early. Even setting the duration to 10 seconds, the completion block gets called immediately.

[UIView animateWithDuration:10.0 animations:^{
message.frame = newFrame;
} completion:^(BOOL finished) {
NSLog(@"DONE???");
}];


Any thoughts on why this is happening? Thanks.

Answer Source

From the UIView documentation:

completion

A block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called. If the duration of the animation is 0, this block is performed at the beginning of the next run loop cycle. This parameter may be NULL.

What this means is that there isn't a guarantee that the code will be executed only when the animation is done. I'd advise you to check the "finished" parameter as a condition for execution.