Stop all animations running in different threads

I have a menu with elements that appear immediately after each other with an interval of 3 seconds, I do it like this:

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 (for example, when a button is pressed)? I tried just setting everything to alpha 1.0, but as expected, the threads continued to work and displayed the elements again.

Thank you for any ideas :)
Shay.

+5
source share
3 answers

You can make animations using block-based animations of the UIView, with a set delay:. Not a single thread is worried.

UIViewAnimationOptionBeginFromCurrentState.

- , , . , , view.layer.presentationLayer, . QuartzCore. removeAllAnimations, , , , .

+3

, , . BOOL, , .

, , buttonTapped.

NO.

- (id)init {
    if (self = [super init]) {
        buttonAlreadyTapped = NO;
    }

    return self;
}

YES.

- (void)buttonTapped {
    self.buttonAlreadyTapped = YES;
}

alpha, NO.

for (NSInteger i = 1; i <= [menuItems count]; i++) {        
    UIButton *menuItem = [menuItems objectAtIndex:i-1];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (3 * i) * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        if (!self.buttonAlreadyTapped) {
            menuItem.alpha = 1.0f;
        }
    });
}

, UIKit .

for (NSInteger i = 1; i <= [menuItems count]; i++) {        
    UIButton *menuItem = [menuItems objectAtIndex:i-1];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (3 * i) * NSEC_PER_SEC), dispatch_get_current_queue(), ^{
        if (!self.buttonAlreadyTapped) {
            dispatch_async(dispatch_get_main_queue(), ^{
                menuItem.alpha = 1.0f;
            });
        }
    });
}
+1

, , , , .

0

All Articles