2015-11-23 56 views
1

我有以下動畫塊,它會旋轉一次按鈕。我想繼續旋轉按鈕,直到refreshPendingRequests方法結束。iOS:重複動畫塊直到事件完成

refreshPendingRequests方法從我的服務器檢索數據,所以我想繼續旋轉按鈕,直到我檢索到數據。

CGFloat direction = 1.0f; // -1.0f to rotate other way 
refreshButton.transform = CGAffineTransformIdentity; 
[UIView animateKeyframesWithDuration:1.0 delay:0.0 
          options:UIViewKeyframeAnimationOptionCalculationModePaced | UIViewAnimationOptionCurveEaseInOut 
          animations:^{ 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformMakeRotation(M_PI * 2.0f/3.0f * direction); 
           }]; 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformMakeRotation(M_PI * 4.0f/3.0f * direction); 
           }]; 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformIdentity; 
           }]; 
          } 
          completion:^(BOOL finished) { 
           [self performSelector:@selector(refreshPendingRequests) withObject:nil afterDelay:0.0]; 
          }]; 

有誰知道是否有某種類型的repeats財產,我可以打電話或直到refreshPendingRequests事件結束後我如何執行此動畫?

+0

爲什麼你的地方refreshPendingRequests來電建成塊?這意味着每秒(因爲你的動畫持續時間是1.0),你將再次調用'refreshPendingRequests'。我懷疑這不是你想要的。 –

+0

@SantaClaus啊絕對不是我想要的,我只想旋轉繼續,直到'refreshPendingRequests'完成。 –

回答

1

確實有這個選項:UIViewKeyframeAnimationOptionRepeat

將該選項傳入,並將nil傳遞給完成塊,因爲您不需要它。

[UIView animateKeyframesWithDuration:1.0 delay:0.0 
          options:UIViewKeyframeAnimationOptionCalculationModePaced | UIViewAnimationOptionCurveEaseInOut | UIViewKeyframeAnimationOptionRepeat 
          animations:^{ 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformMakeRotation(M_PI * 2.0f/3.0f * direction); 
           }]; 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformMakeRotation(M_PI * 4.0f/3.0f * direction); 
           }]; 
           [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.0 animations:^{ 
            refreshButton.transform = CGAffineTransformIdentity; 
           }]; 
          } 
          completion:nil]; 

那麼當你刷新完成:

[refreshButton.layer removeAllAnimations];