简体   繁体   English

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

[英]UIView Animation doesn't run in correct order in for loop - iOS

I have a simple for loop which animates some UILabels. 我有一个简单的for循环,它对一些UILabel进行了动画处理。 The reason I have the animation block code in my for loop is because obviously I have more than one label I am trying to animate. 我的for循环中包含动画块代码的原因是,显然,我有多个要尝试制作动画的标签。

Now my program increments the numbers in the UILabels by one every time AFTER the ENTIRE for loop has finished. 现在,在整个for循环结束后,我的程序每次将UILabel中的数字加1。 However by the time the animation happens, the number has already been incremented (which is not what I want), even though the number increment code comes AFTER the for loop.... 但是,到动画发生时,数字已经增加了(这不是我想要的),即使数字增加代码在for循环之后出现。

I'm not sure if I have explained the problem correctly, but in ensconce what I am asking, since we know for a FACT that animations run on the main thread, how can we ensure that the animations happen in the correct order (while in a for loop)??? 我不确定我是否正确解释了这个问题,但是在我所要求的范围内,因为我们知道动画在主线程上运行,因此我们如何确保动画以正确的顺序发生(在一个for循环)???

Here is my code: 这是我的代码:

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

Thanks for your time, Dan. 丹,谢谢您的时间。

The issue is that you're accessing "loop" from inside both blocks. 问题是您要从两个块内部访问“循环”。 Why don't you try running the loop inside of the animation blocks? 为什么不尝试在动画块内部运行循环? Something like this: 像这样:

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

I hope that helps! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM