UIView动画在for循环中没有正确的顺序运行 - iOS

时间:2014-05-22 17:33:04

标签: ios cocoa-touch for-loop objective-c-blocks uiviewanimation

我有一个简单的for循环,可以激活一些UILabel。我在for循环中使用动画块代码的原因是因为很明显我有一个以上的标签,我试图制作动画。

现在,我的程序每次在ENTIRE for循环结束后将UILabels中的数字增加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;
                }];
            }
        }

谢谢你的时间,Dan。

1 个答案:

答案 0 :(得分:1)

问题在于您正在访问&#34;循环&#34;从两个街区内部。为什么不尝试在动画块中运行循环?像这样:

[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;
        }
    }
}];

我希望有所帮助!