2011-08-03 40 views
4

我正在做一些使用块级技术的动画。我想根据一定的条件停止动画。我将如何阻止动画完成?如何停止正在运行的动画

以下是我的动画代码:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         if([arrAns count]>0) 
          vwb1.center = CGPointMake(260, 40); 
        } 
        completion:^(BOOL finished){ 
         [UIView animateWithDuration:1.5 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction 
              animations:^{ 
               if([arrAns count]>1) 
                vwb2.center = CGPointMake(260, 100); 
              } 
              completion:^(BOOL finished){ 
               [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction 
                   animations:^{ 
                    if([arrAns count]>2) 
                     vwb3.center = CGPointMake(260, 160); 
                   } 
                   completion:^(BOOL finished){ 
                    [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction 
                        animations:^{ 
                         if([arrAns count]>3) 
                          vwb4.center = CGPointMake(260, 220); 
                        } 
                        completion:^(BOOL finished){ 
                         [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{ 
                          if([arrAns count]>4) 
                           vwb5.center = CGPointMake(260, 280); 
                         } completion:nil]; 
                        }]; 
                   }]; 

              } 
          ]; 

        }]; 

我没有任何这里的关键,所以我可以n不是将其删除一个关键的基础上。还有一个功能是removeallanimation,但我无法使用它。如果有人能够帮助我如何使用它,它也会很好。

编辑:好吧,我想通了,如何做到这一点,我使用下面的代码做它:

[self.view.layer removeAllAnimations]; 

但现在我有问题尚未开始了我的块级动画仍在运行。所以如果我在第二个块中删除动画,它仍然会执行剩余的块。我也需要阻止它。

感谢
潘卡

+1

你试过removeallanimations? –

+0

@Praveen现在请检查我的信息。我有更新... – pankaj

+0

你必须单独停止所有图层的动画。 –

回答

2

您应该检查finished变量。并且只有在前一个完成时(finished == YES),才会开始下一个动画(块内),而不是停止。

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

...

完成
A嵌段对象被动画序列 结束时执行。该块没有返回值,并采用一个布尔值 参数,该参数指示在调用完成处理程序之前动画是否实际完成 。如果动画为 的时间为0,则此块在 下一个运行循环周期的开始处执行。