简体   繁体   中英

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

I have a simple for loop which animates some UILabels. 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.

Now my program increments the numbers in the UILabels by one every time AFTER the ENTIRE for loop has finished. 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....

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)???

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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