iphone – 如何停止已经运行的动画

我正在使用块级技术做一些动画.我想在某些条件的基础上停止动画.如何阻止动画完成?

以下是我的动画代码:

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

                                          }
                          ];

                     }];

我没有任何钥匙,所以我不能在钥匙的基础上删除它.还有一个功能,如removeallanimation但我无法使用它.如果有人可以帮助我如何使用它也会没事的.

编辑:好的我想出了如何做到这一点,我正在使用以下代码:

[self.view.layer removeAllAnimations];

但是现在我的问题是我的块级动画还没有开始运行.因此,如果我在第二个块中删除动画,它仍会执行剩余块.我也需要阻止它.

谢谢
潘卡

最佳答案 你应该检查完成的变量.只有先前完成(完成==是),才开始下一个动画(在一个块内),而不是停止.

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

completion
A block object to be executed when the animation sequence
ends. This block has no return value and takes a single Boolean
argument that indicates whether or not the animations actually
finished before the completion handler was called. If the duration of
the animation is 0, this block is performed at the beginning of the
next run loop cycle.

点赞