3

i use the following code to do a string rolling, but how to 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];  
www.ruu.cc
  • 445
  • 1
  • 6
  • 16

1 Answers1

1
- (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 header:

BOOL keepAnimating;

To start animation:

keepAnimating = YES;
[self doAnimation];

To stop animation:

keepAnimating = NO;

This solution uses the block-based animation methods on UIView. Concerning the old set of animation methods ( [UIView beginAnimations] etc ) the UIView class reference states:

Use of the methods in this section is discouraged in iOS 4 and later. Use the block-based animation methods instead.

Jacob Jennings
  • 2,796
  • 1
  • 24
  • 26