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.
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.
Yes. It is being called too early because it's being interrupted somehow. Probably by a modal presentation transition or perhaps something else. Depending on your needs, the following may be a solution you like. We avoid the conflict by manually delaying the execution of our animation code like so:
// To get this in Xcode very easily start typing, "dispatch_aft..."
// Note the "0.2". This ensures the outstanding animation gets completed before we start ours
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:1.0 delay:0 options:0 animations:^{
// Your animation code
} completion:^(BOOL finished) {
// Your completion code
}];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With