How to stop an already running animation

I am doing animation using the block level technique. I want to stop the animation based on a specific state. How can I stop the animation?

Below is my animation code:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ if([arrAns count]>0) vwb1.center = CGPointMake(260, 40); } completion:^(BOOL finished){ [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ if([arrAns count]>1) vwb2.center = CGPointMake(260, 100); } completion:^(BOOL finished){ [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ if([arrAns count]>2) vwb3.center = CGPointMake(260, 160); } completion:^(BOOL finished){ [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ if([arrAns count]>3) vwb4.center = CGPointMake(260, 220); } completion:^(BOOL finished){ [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ if([arrAns count]>4) vwb5.center = CGPointMake(260, 280); } completion:nil]; }]; }]; } ]; }]; 

I have no key here, so I cannot delete it based on the key. There is also a removeallanimation function, but I cannot use it. If someone can help me on how to use this, everything will be fine.

Edit: Ok, I figured out how to do this, I am doing this using the following code:

 [self.view.layer removeAllAnimations]; 

But now I have a problem that my block level animation, which is not running yet, is still running. Therefore, if I delete the animation in the second block, it still executes the remaining block. I also need to stop this.

thanks
Pankai

+4
source share
1 answer

You should check the finished variable. And start the next animation (inside the block) only if the previous one is finished ( finished == YES ) and not stopped.

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

...

Completion
The block object that should be executed when the animation sequence ends. This block has no return value and takes a single boolean argument indicating whether the animation was actually before the completion handler was called. If the animation duration is 0, this block is executed at the beginning of the next cycle of the cycle cycle.

+2
source

All Articles