Supertecnoboff
Supertecnoboff

Reputation: 6606

Stop specific UIViewAnimation block - iOS

I have an iOS application which runs a few different UIViewAnimation blocks to animate a few different objects on screen. This all works, but how can I stop ONE of the animation blocks WITHOUT stopping the rest?

I have tried using the following method:

[button.layer removeAllAnimations];

But it doesn't do anything, the animation just continues.

I then tried to use a simple BOOL value and get the animation to return from the method once the BOOL is set to "NO", but that doesn't work either.

Here is the animation which I am trying to stop:

-(void)undo_animation:(int)num {

    // Fade out/in the number button label
    // animation which lets the user know
    // they can undo this particular action.

    [UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionRepeat | UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{

        // Mostly fade out the number button label.
        ((UIButton *)_buttons[num]).alpha = 0.2;

    } completion:^(BOOL finished) {

        [UIView animateWithDuration:0.5 delay:0.2 options:(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{

            // Fade in the number button label.
            ((UIButton *)_buttons[num]).alpha = 1.0;

        } completion:^(BOOL finished) {

            // Stop the animation if the BOOL
            // is set to 'NO' animations.

            if (anim_state_button == NO) {
                return;
            }
        }];
    }];
}

Thanks, Dan.

Upvotes: 2

Views: 367

Answers (3)

mert
mert

Reputation: 1098

You can't reach running animation property if you're using UIKit Animations. So I suggest to using Core Animation if you want to modify animation in the runtime of it.

And it's too simple to removing alpha of the view like below.

CABasicAnimation* fadein= [CABasicAnimation animationWithKeyPath:@"opacity"];
[fadein setToValue:[NSNumber numberWithFloat:1.0]];
[fadein setDuration:0.5];
[[moviepic layer]addAnimation:fadein forKey:@"MyAnimation"]; 

after adding animation to layer animation will start and than you can use delegate methods to be informed about animationDidFinish: method

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
    NSLog(@"Animation interrupted: %@", (!flag)?@"Yes" : @"No");
}

Also you can reach whenever you want from using;

[[moviepic layer] animationForKey:@"MyAnimation"];

And of course you need to add CoreAnimation Framework to your project.

Hope it helps.

Upvotes: 2

Samuel W.
Samuel W.

Reputation: 380

My understanding is that removing all animations from the relevant layer should stop all animations. How about [((UIButton *)_buttons[num]).layer removeAllAnimations]; ?

Upvotes: 0

Muhammad Waqas Bhati
Muhammad Waqas Bhati

Reputation: 2805

I think the simple way is that remove all animations from your View Layer as all the animation are by default added into the View's Layer.

[yourRequiredView.layer removeAllAnimations]; 

Upvotes: 1

Related Questions