开发者

how to stop animation after using beginAnimations?

开发者 https://www.devze.com 2022-12-23 07:05 出处:网络
i use the following code to do a string rolling, but how to stop the animation? [UIView beginAnimations:@\"ruucc\" context:NULL];

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];  


- (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号