2011-08-03 41 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,則此塊在 下一個運行循環週期的開始處執行。