2

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.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98

3 Answers3

2

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.

mert
  • 1,090
  • 13
  • 26
1

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]; 
Muhammad Waqas Bhati
  • 2,775
  • 20
  • 25
0

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

Samuel W.
  • 370
  • 1
  • 10
  • if you using uiview animation you're not adding it to layer that's why it's not possible to remove animations from layer. – mert Jul 08 '15 at 07:24
  • 1
    @mert. It's correct that you (the programmer) are not directly adding animations to any layer, but my impression was that UIView animations do in fact add CAAnimations to any relevant layer behind the scenes. – Samuel W. Jul 10 '15 at 05:33
  • After you response I dug into UIView's mechanic and you were right indeed UIView animations is using CALayer to make changes. Found this from apple docs _Because iOS views always have an underlying layer, the UIView class itself derives most of its data from the layer object directly_ – mert Jul 11 '15 at 01:32