动画在iOS中分离

时间:2012-02-19 09:53:19

标签: ios xcode animation uiimageview core-animation

我有一个简单UIView的应用。此UIImageViews上有一个UIview的矩阵NxN。

我想转动每个UIImageView。但是一个接一个。我试过这段代码:

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

但所有图像都在同一时间旋转。

我该如何分开这个动画?

UPD

现在我将此代码用于更复杂的动画:

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

用于某些矩阵'部分的同步旋转(其中poleBeforeTurn[k][l] == 1)。但是这段代码首先在一次中旋转所有部分(比如我只在循环中使用pole[k][l].imageView.transform = CGAffineTransformMakeRotation(1.57079633*posTo);而没有动画),然后再实现动画。

我应该如何避免第一次旋转并仅实现动画?

3 个答案:

答案 0 :(得分:2)

尝试为每个UIImageView动画设置延迟:

[UIView setAnimationDelay:delay];

如果在每次循环后增加延迟的持续时间,您应该获得所需的效果。

答案 1 :(得分:2)

beginAnimations:context:的第一个参数是animationID。您可以使用它来分组(或在您的情况下取消组合)动画。

尝试为每个对象使用不同的animationID

float delay = 0;
for (int k=0; k<kol; k++)
    for (int l=0; l<kol; l++)
    {
        [UIView beginAnimations:[NSString stringWithFormat:@"anim_%d_d", k, l] context:NULL];
        [UIView setAnimationDelay:delay];
        /* animate */
        delay += 0.05;
    }
}

答案 2 :(得分:0)

尝试NSTimer https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSTimer_Class/Reference/NSTimer.html#//apple_ref/doc/uid/TP40003747

为单元格的动画编写一个方法(单元格的coords将是params)并在不同的调用之间放置一个计时器