1

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.

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98

1 Answers1

1

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!

Rob
  • 1,025
  • 2
  • 10
  • 27
  • 1
    I should mention that in your code, it is imposible to predict the order of execution for your instructions. In the solution I provide, all your labels will animate at the same time. I am a bit confused on why you are changing your properties in the animation and them setting them to something else in the completion block? Based only on the alpha property, your labels will all fade out and then abruptly appear again. – Rob May 22 '14 at 17:42
  • While your code runs smoother it doesn't seem to stop the in sync effect. I noticed that when I removed the animation out side the for loop everything seems to run in sync. The only issue now is that I have lots of if statements and copies of code.... :( Do you think the for loop delays the animation or something? – Supertecnoboff May 22 '14 at 18:07
  • I'm a bit confused on what you are trying to accomplish. Are you trying to get all your labels to move simultaneously, or are you wanting them to move one at a time? – Rob May 22 '14 at 18:26
  • Well basically I am changing the numbers in these UILabels AFTER the animation happens (well thats the order of my code). But weirdly, my UILabels seem to change and THEN the animation happens which is not what I want and more to the point is NOT the order of my code. – Supertecnoboff May 23 '14 at 09:56
  • I managed to figure it out. I tried separating the code which changes the UILabels into another method and then ran that method from the completion handler of the UIView Animations. So it all works. I will tick you answer though as it seems to produce a more smooth animation with no delay. Thank you. – Supertecnoboff May 23 '14 at 20:20