IOS - Stop repeating UIAnimation?

I have an animation that needs to be repeated until I stop it.

How do I stop animation after clicking a button?

[UIView animateWithDuration:0.2 delay:0 options:(UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat) animations:^{

        CGAffineTransform transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(5));
        self.transform = transform;

    }  completion:^(BOOL finished){




source to share

2 answers

U can use CABasicAnimation.

    CABasicAnimation *appDeleteShakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
appDeleteShakeAnimation.autoreverses = YES;
appDeleteShakeAnimation.repeatDuration = HUGE_VALF;
appDeleteShakeAnimation.duration = 0.2;
appDeleteShakeAnimation.fromValue = [NSNumber numberWithFloat:-degreeToRadian(5)];
appDeleteShakeAnimation.toValue=[NSNumber numberWithFloat:degreeToRadian(5)];
[self.layer addAnimation:appDeleteShakeAnimation forKey:@"appDeleteShakeAnimation"];


Then when u wants to stop it you can just call

[self.layer removeAnimationForKey:@"appDeleteShakeAnimation"];




You need to add one more option UIViewAnimationOptionAllowUserInteraction


You should try the following:

[UIView animateWithDuration:2 delay:0 options:UIViewAnimationCurveLinear | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction animations:^
    view.frame = CGRectMake(0, 100, 200, 200);
} completion:^(BOOL finished)
    if(! finished) return;


And to stop the animation use this:

[view.layer removeAllAnimations];




All Articles