2016-01-24 84 views
0

我有淡入的方法的動畫目標C完成塊,然後淡出按鈕標籤(如下所示):對於內for循環(暫停循環處理)

-(void)fade:(NSString *)text 
{ 

    NSLog(@"Starting Fade In for symbol %@",text); 
    [self.symbolButton setAlpha:0.0f]; 
    [self.symbolButton setTitle:text forState:UIControlStateNormal]; 
    [UIView animateWithDuration:2 animations:^{ 
     [self.symbolButton setAlpha:1.0f]; 

    } completion:^(BOOL finished) { 

    NSLog(@"Starting Fade Out for symbol %@",text); 
    [UIView animateWithDuration:2 animations:^{ 
    [self.symbolButton setAlpha:0.0f]; 
    } completion:nil]; 
    }]; 
} 

這按預期工作。然而,我不想單個字符串,而是想循環一個數組的內容(例如淡入「A」,淡出「A」,淡入「B」,淡出「B」。 ...)。

在上面的代碼中,完成塊等待淡入完成後纔開始淡出(良好)。但是,如果我嘗試使用for循環處理數組(修改方法以在迭代數組的for循環中接受數組和嵌套操作),循環立即循環,而不是等待第一個字符完成。

有沒有辦法在第二個動畫動作中使用完成塊來暫停循環的處理 - 或者我以錯誤的方式解決這個問題?

+1

遞歸可以是一個解決方案,但在現代編程範例中,使用遞歸是不好的。完成時調用相同的函數。 –

回答

0

你可以試試這個。我沒有測試它,但這個想法應該工作

NSArray<UIButton*>* arrButtons; 
NSString* text; 

for (int i = 0 ; i < arrButtons.count; i++) { 
    UIButton* b = arrButtons[i]; 
    b.alpha = 0.0f; 
    [b setTitle:text forState:UIControlStateNormal]; 

    [UIView animateKeyframesWithDuration:2 delay:4*i options:0 animations:^{ 

     b.alpha = 1.0f; 

    } completion:^(BOOL finished) { 

     NSLog(@"Starting Fade Out for symbol %@",text); 

     [UIView animateWithDuration:2 animations:^{ 
      b.alpha = 0.0f; 
     } completion:nil]; 

    }]; 

}