Search code examples
iosxcodeanimationuiimageviewcore-animation

Animation separating in iOS


I got an app with a simple UIView. There is a matrix NxN of UIImageViews on this UIview.

I wanna turn each UIImageView. But one by one. I tried this code:

for (int k=0; k<kol; k++)
        for (int l=0; l<kol; l++)
        {
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
                int posTo = pole[k][l].position ;
                pole[k][l].position++;
                if (posTo == 4) {posTo=0; pole[k][l].position=1;}
                pole[k][l].imageView.transform = CGAffineTransformMakeRotation(1.57079633*posTo);
            [UIView commitAnimations];
        }

But all images rotate in the same time.

How should i separate this animation?

UPD

Right now i use this code for more complicated animation:

-(void) turnAllPole
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelay:delay];
    for (int k=0; k<kol; k++)
        for (int l=0; l<kol; l++)
        {
            if (poleBeforeTurn[k][l] == 1)
            {
                int posTo = pole[k][l].position ;
                pole[k][l].position++;
                if (posTo == 4) {posTo=0; pole[k][l].position=1;}
                pole[k][l].imageView.transform = CGAffineTransformMakeRotation(1.57079633*posTo);
           }    
        }
    [UIView commitAnimations];
    delay += 0.3;
}

that's for synchronized rotation of some matrix' parts (where poleBeforeTurn[k][l] == 1). But this code firstly rotate all parts in one time (like if i use in loops only pole[k][l].imageView.transform = CGAffineTransformMakeRotation(1.57079633*posTo); without animation) and after that implement animation.

How should i avoid first rotation and implement only animation?


Solution

  • Try to set a delay to each of the UIImageView animations:

    [UIView setAnimationDelay:delay];
    

    If you increment the duration of the delay after every loop you should get the desired effect.