2016-01-24 87 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]; 

    }]; 

}