How to stop animation after using beginAnimations?

I use the following code to rewind a line, but how do I stop the animation?

[UIView beginAnimations:@"ruucc" context:NULL];
[UIView setAnimationDuration:12.8f];  
[UIView setAnimationCurve:UIViewAnimationCurveLinear];  
[UIView setAnimationDelegate:self];  
[UIView setAnimationRepeatAutoreverses:NO];  
[UIView setAnimationRepeatCount:999999]; 

frame = label1.frame;
frame.origin.x = -12;
label1.frame = frame;
[UIView commitAnimations];  
+3
source share
1 answer
- (void)doAnimation
{
    [UIView animateWithDuration:12.8f
                          delay: 0.0
                        options: UIViewAnimationCurveLinear
                     animations:^{
                         frame = label1.frame;
                         frame.origin.x = -12;
                         label1.frame = frame;
                     }
                     completion:^(BOOL finished){
                         if(keepAnimating) {
                             [self doAnimation];
                         }
                     }]; 
}

In the title:

BOOL keepAnimating;

To start the animation:

keepAnimating = YES;
[self doAnimation];

To stop the animation:

keepAnimating = NO;

This solution uses block animation methods in UIView. Regarding the old set of animation methods ([UIView beginAnimations], etc.), the reference to the UIView class states:

Using the methods in this section is not recommended in iOS 4 and later. Use block-based animation methods instead.

+1
source

Source: https://habr.com/ru/post/1737775/


All Articles