如何在使用beginAnimations后停止动画?

时间:2010-03-20 02:38:57

标签: cocoa-touch

我使用以下代码进行字符串滚动,但是如何停止动画?

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

1 个答案:

答案 0 :(得分: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];
                         }
                     }]; 
}

在标题中:

BOOL keepAnimating;

开始动画:

keepAnimating = YES;
[self doAnimation];

停止动画:

keepAnimating = NO;

此解决方案在UIView上使用基于块的动画方法。关于旧的动画方法集([UIView beginAnimations]等),UIView类引用声明:

在iOS 4及更高版本中不鼓励使用本节中的方法。改为使用基于块的动画方法。