Start UIView animation block when another animation starts

I use the basic block API for my animations in iOS.

One animation has a completion block, and this block is called at the end of the animation.

However, this animation can be run several times when the user scrolls (the animation is on a UITableViewCell ). When this happens, the completion block is called several times. The finished block parameter is always YES .

Since the animation was not actually completed (another animation occurred), I thought that the finished parameter would be NO, but it is not.

Did I miss something? How can I avoid executing a completion block that will be called multiple times?

+4
source share
3 answers

I solved this problem by looking to see if the completion block is relevant at the beginning of this block.

finished is irrelevant right now. I talked with Apple and was told that this was fixed in iOS 4.2.

0
source

The completion block is called several times simply because, in your case, your animation runs several times. What happens is that iOS calls your animation block every time it is told this, probably in a separate thread. Then, for each animation, it tracks its completion, and upon completion, calls the corresponding completion block. Thus, you see that your completion block fires several times, one for each call to your animation. Please note that the boolean value associated with the completion block is specific to this completion block, it does not apply to any other animation.

Recall that you are just experiencing the concurrency effect. If this is not your intentional behavior, you need to modify your code accordingly. If you want your animations to run one at a time, you can use NSLock (NSConditionLock for advanced control using a conditional variable) or, if you want, the mutex and the Posix pthreads library directly to create a critical section to execute in different ways.

+5
source

Not sure when you shoot an animation and do it loop (for example, using a UIActivityView counter or something else) - does each pixel viewed in a table sound?

In any case, perhaps you could use the UIScrollView delegation methods and tell each cell about the start of the animation on scrollViewWillBeginDragging: and tell each cell about the completion in scrollViewDidEndDragging:

You can set boolean isAnimating for your UITableViewCell , and if the animation is currently running, do nothing.

 if (isAnimating) { // ... do nothing } else { // Start your animation } 

Or stick with what you have now and use boolean, but only tan the animation if it does not currently enliven. Then in your finished parameter just set isAnimating to NO .

 if (isAnimating) { // ... do nothing } else { [UIView animateWithDuration:0.3f animations:^{ // animations... isAnimating = YES; } completion:^{ isAnimating = NO; } ]; } 
+2
source

All Articles