I'm running this animation on a user interaction, and sometimes the animation may be run again before the ongoing animation has finished. What I would like it to do is to cancel the previous animation and continue with the new one.
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.bounds = bounds;
}
completion:^(BOOL finished) {
if (finished) {
// Do some cleanup after animating.
}
}];
Visually, this seems to work, but in my completion block I am told that it finished in both cases, which causes the cleanup code to run prematurely. So the first animation's completion block runs immediately after the second one starts, with finished = YES
. I expected it to have a finished value of NO
and the second one (once it completes) to have YES
.
Is there a way to know if the animation completed or if it was cancelled by another?
Sidenote: I tried doing the same animation with CABasicAnimation
and then I get finished = NO
the first time and YES
the second time, so the behavior I'm getting seems to be specific to animateWithDuration
.
Here's a GIF showing the above code in action with a duration of 10 and the completion block updating the label. As you can see, finished
is YES
every time the animation is restarted with the animateWithDuration
call: