Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping all animations being performed on different thread

I have a menu with items popping right after each other in intervals of 3 seconds, I'm doing it like so:

for(UIButton *menuItem in menuItems){
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (0.3 * i) * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        [menuItem setAlpha:1.0];
    }                             
}

Is it possible to stop the animation in the middle (when a button is touched, for example) ? I tried just setting everything to alpha 1.0 but as expected, the threads kept running and shows the items again.

Would appreciate any ideas:)
Shai.

like image 751
Shai Mishali Avatar asked Oct 10 '22 13:10

Shai Mishali


1 Answers

You could do the animations using UIView block-based animations, with the delay: set. No thread worries there.

These animations can be interrupted by starting another block-based animation with the option UIViewAnimationOptionBeginFromCurrentStateset.

For alpha transitions this is fine since it doesn't matter what the current position of the animation is. If you are moving objects, you can get the current position by querying the view.layer.presentationLayer which holds the current state of the object. You will need to import the QuartzCore framework to do this. This also gives you a method, removeAllAnimations which will do pretty much what it sounds like and takes any view right to the endpoint of any animations it has got pending or active.

like image 177
jrturton Avatar answered Oct 13 '22 11:10

jrturton