[英]UIView Animation doesn't run in correct order in for loop - iOS
我有一个简单的for循环,它对一些UILabel进行了动画处理。 我的for循环中包含动画块代码的原因是,显然,我有多个要尝试制作动画的标签。
现在,在整个for循环结束后,我的程序每次将UILabel中的数字加1。 但是,到动画发生时,数字已经增加了(这不是我想要的),即使数字增加代码在for循环之后出现。
我不确定我是否正确解释了这个问题,但是在我所要求的范围内,因为我们知道动画在主线程上运行,因此我们如何确保动画以正确的顺序发生(在一个for循环)???
这是我的代码:
for (int loop = 0; loop < [number_arrange count]; loop++) {
if ([number_arrange[loop] integerValue] == checknum) {
[anim_check replaceObjectAtIndex:loop withObject:[NSNumber numberWithInteger:1]];
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(5.0, 5.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, -20.0);
((UILabel*)_labels_anim[loop]).alpha = 0.0;
} completion:^(BOOL finished) {
((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(1.0, 1.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, 20.0);
((UILabel*)_labels_anim[loop]).alpha = 1.0;
}];
}
}
丹,谢谢您的时间。
问题是您要从两个块内部访问“循环”。 为什么不尝试在动画块内部运行循环? 像这样:
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
for (int loop = 0; loop < [number_arrange count]; loop++) {
if ([number_arrange[loop] integerValue] == checknum) {
[anim_check replaceObjectAtIndex:loop withObject:[NSNumber numberWithInteger:1]];
((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(5.0, 5.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, -20.0);
((UILabel*)_labels_anim[loop]).alpha = 0.0;
}
}
} completion:^(BOOL finished) {
for (int loop = 0; loop < [number_arrange count]; loop++) {
if ([number_arrange[loop] integerValue] == checknum) {
((UILabel*)_labels_anim[loop]).transform = CGAffineTransformMakeScale(1.0, 1.0);
((UILabel*)_labels_anim[loop]).layer.anchorPoint = CGPointMake(0.5, 0.5);
((UILabel*)_labels_anim[loop]).frame = CGRectOffset(((UILabel*)_labels_anim[loop]).frame, 0.0, 20.0);
((UILabel*)_labels_anim[loop]).alpha = 1.0;
}
}
}];
希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.