我有一个淡入的方法,然后淡出一个按钮标签(如下所示):
-(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 循环中嵌套操作),循环会立即循环,而不是等待第一个字符完成。
有没有办法在第二个动画操作中使用完成块来暂停循环的处理 - 还是我以错误的方式处理?
你可以
试试这个。我没有测试它,但这个想法应该有效
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];
}];
}