cocoa-touch - 使用 beginAnimations 后如何停止动画?

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

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

在标题中:
BOOL keepAnimating;

开始动画:
keepAnimating = YES;
[self doAnimation];

停止动画:
keepAnimating = NO;

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

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

https://stackoverflow.com/questions/2481695/

相关文章:

path - 如何在贝塞尔路径上进行几何高级操作?

django - 在templatetags之间传递上下文,Django

ruby-on-rails - Rails 3发布后对Rails 2的支持

asp.net - 跨子域的ASP.NET MVC session

spring - 如何在库的上下文中实例化 Spring?

python-3.x - 从字符串生成用于加密的整数,反之亦然

asp.net - 如何在 Web 应用程序中对调用 Web 服务的 Windows Mobile

svg - 扩展Zedgraph以生产SVG

.net - 将.NET程序集移离应用程序基本目录?

rest - 在RESTful架构中实现类似批处理操作的最佳方法?