2011-04-28 56 views
9

我想知道如何設置動畫重複。重複次數需要由一個變量決定。在以下代碼中,變量int newPage應確定動畫重複的頻率。重複動畫不定次數

我想這一點,但動畫(它採用了塊動畫)只執行一次:

for (int temp = 1; temp <= newPage; temp++) { 
      [self animatePage]; 
} 

如果我將以下代碼,它就像我希望它,但這是硬編碼(即動畫將重複兩次),我看不出如何改變多久這個動畫代碼,根據我的變量NEWPAGE執行和數量的方式:

[UIView animateWithDuration:0 
         delay:0.1 
      options:UIViewAnimationOptionCurveEaseIn 
      animations:^{[self animatePage];} 
      completion:^(BOOL finished){[self animatePage];}]; 

,我會非常感激爲如何重複相同的動畫而不必硬編碼的建議我希望這個動畫重複的次數。


編輯:

我試圖執行下面的代碼,但只有一個動畫將實際進行:

 [UIView animateWithDuration:0 
          delay:1 
         options:UIViewAnimationOptionCurveEaseIn 
        animations:^{ 

         [UIView setAnimationRepeatCount:2]; 
         [self animatePage]; 

        } 
        completion:nil]; 
+0

你知道,我相當肯定「removeAllAnimations」就是你在這裏之後 - 看下面的答案,歡呼! – Fattie 2014-03-07 16:24:58

回答

28

有同樣的問題,以發揮 - 你缺少an'UIViewAnimationOptionRepeat」

這應該工作:

[UIView animateWithDuration:0 
         delay:1 
        options:UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat 
       animations:^{ 
        [UIView setAnimationRepeatCount:2]; // **This should appear in the beginning of the block** 
        [self animatePage]; 

       } 
       completion:nil]; 

的伎倆對我來說。

7

你嘗試設置repeatCount? + (void)setAnimationRepeatCount:(float)repeatCount

我試過下面的代碼塊,它肯定重複2X我(L是在X DIR和3X Y中DIR放大2倍由一個UITextView):

[UIView animateWithDuration:2 
delay:0.1 
options:UIViewAnimationOptionCurveEaseIn 
animations:^{ [UIView setAnimationRepeatCount:2]; 
l.transform = CGAffineTransformMakeScale(2,3); } completion:nil]; 
+0

沒有好主意!但是,我將如何實施這個?對不起,如果這是基本的,但我對這一切都比較陌生。 – 2011-04-28 18:16:33

+0

@DavidNeiss:我剛剛嘗試過,但不起作用。如果將其設置爲2,則只會執行一個動畫。我相應地更新了上面的代碼,所以你可以看到我是如何實現它的。謝謝你的幫助。 – 2011-04-28 18:42:42

+0

@David:你應該將該代碼編輯到你的答案中。 – 2011-04-28 18:42:56

2

的原因你的arent看到,但一個動畫是由於您呼叫來自同一runloop的循環動畫,在最後一次通話resuting獲獎(一個動畫)

而不是調用[self animatePage]的,嘗試調用

[self performSelector:@selector(animatePage) withObject:nil afterDelay:.1 *temp]; 

這將在單獨的線程上創建您的呼叫。

您可能需要延遲間隔

+0

我明白你的意思了,但這不適用於短時間間隔。我真的需要0.1和你建議的代碼行只適用於1.0及以上(這是太慢)。儘管謝謝你的澄清! – 2011-04-28 18:36:39

+2

這不_not_創建一個單獨的線程。 – 2011-04-28 18:41:59