我正在解决我的问题。做我的循环我将动画参数保存在表中
NSArray *animation1 = [[NSArray alloc]initWithObjects:[NSNumber numberWithInt:label1.tag], [NSNumber numberWithInt:3],[NSNumber numberWithFloat:0.5f*time],[NSNumber numberWithFloat:timeDelay],[NSNumber numberWithFloat:0],nil];
[animations addObject:animation1];
在 void 结尾我调用 [self performAnimations];
- (void)performAnimations {
if ([[self animations] count] == 0) return;
for (int i=0; i<[[self animations] count]; i++) {
int tag = [[[animations objectAtIndex:i]objectAtIndex:0]intValue];
int animation = [[[animations objectAtIndex:i]objectAtIndex:1]intValue];
float animTime = [[[animations objectAtIndex:i]objectAtIndex:2]floatValue];
float animDelay = [[[animations objectAtIndex:i]objectAtIndex:3]floatValue];
float parameter = [[[animations objectAtIndex:i]objectAtIndex:4]floatValue];
UILabel *label = [UILabel alloc];
UIView *view = [UIView alloc];
if ([[self.view viewWithTag:tag] isKindOfClass:[UILabel class]]){
label = (UILabel *)[self.view viewWithTag:tag];
} else if ([[self.view viewWithTag:tag] isKindOfClass:[UIView class]]) {
view = (UIView *)[self.view viewWithTag:tag];
}
switch (animation) {
case 1:
[PolyCalcsViewController colorizeLabelForAWhile:label withUIColor:[UIColor yellowColor] animated:YES withTime:animTime withDelay:animDelay];
break;
case 2:
[PolyCalcsViewController appear:label withTime:animTime withDelay:animDelay];
break;
case 3:
[PolyCalcsViewController disappear:label withTime:animTime withDelay:animDelay];
break;
case 4:
[PolyCalcsViewController moveView:view withOffset:parameter withTime:animTime withDelay:animDelay];
break;
default:
break;
}
}
[animations removeAllObjects];
}
+(void)colorizeLabelForAWhile:(UILabel *)label withUIColor:(UIColor *)tempColor animated:(BOOL)animated withTime:(float)time withDelay:(float)delay{
UILabel *tempLabel = [[UILabel alloc] init];
tempLabel.textColor = tempColor;
tempLabel.font = label.font;
tempLabel.alpha = 0;
tempLabel.textAlignment = label.textAlignment;
tempLabel.text = label.text;
tempLabel.backgroundColor = [UIColor clearColor];
[label.superview addSubview:tempLabel];
tempLabel.frame = label.frame;
if (animated) {
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it
label.alpha = 0;
tempLabel.alpha = 1;
} completion:^(BOOL finished){
[UIView animateWithDuration:time
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
// Animate it back.
label.alpha = 1;
tempLabel.alpha = 0;
} completion:^(BOOL finished){
// Remove the tempLabel view when we are done.
[tempLabel removeFromSuperview];
}];
}];
} else {
// Change it back at once and remove the tempLabel view.
label.alpha = 1.0;
[tempLabel removeFromSuperview];
}
}
+(void)appear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 1.0;
} completion:^(BOOL finished){
NSLog(@"Anim Appear %d",label.tag);
}];
}
+(void)disappear:(UILabel *)label withTime:(float)time withDelay:(float)delay{
[UIView animateWithDuration:time
delay:delay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished){
NSLog(@"Anim Disappear %d",label.tag);
[label removeFromSuperview];
}];
}
关键是程序没有按照表格动画中的顺序执行动画。我想我应该放弃时间延迟,在上一个动画完成后运行下一个动画。我可以在 completion:(BOOL finished) 块中创建一个计数器,但我不知道如何,因为该函数不接受外部变量。任何想法如何检测动画结束并控制表格执行?谢谢。