我有一个方法可以淡入,然后淡出按钮标签(如下所示):
-(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循环中嵌套操作),该循环将立即循环,而不是等待第一个字符完成。
有没有一种方法可以在第二个动画动作中使用完成块来暂停循环的处理-或者我做错了吗?
发布于 2016-01-25 02:55:59
你可以试试这个。我没有测试它,但是这个想法应该行得通
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];
}];
}https://stackoverflow.com/questions/34978661
复制相似问题