0

I have an array of UILabels that I want to animate across the screen. I am iterating though the array using a for loop like below:

for(int i=0; i<[self.onScreenLabels count]; i++)
{
    UILabel *label = self.onScreenLabels[i];

    int x = label.frame.origin.x;
    int y = label.frame.origin.y;

    label.center=CGPointMake(320, 0);
    [self.view addSubview:label];

    [UIView animateWithDuration:0.3 delay:1.0 options:0 animations:^{

    label.center=CGPointMake(x, y);

} completion:^(BOOL finished){

}];

}

I want each UILabel to delay 1.0 second after the last before animating, e.g. wait 1 second, fire the first, wait 1 second fire the second, wait 1 second fire the third ....etc all the way to the end of the array. However using this code there is a 1 second delay but ALL the labels then animate at the same time. Why is this? Is there a way around it?

thanks

4

2 回答 2

1

延迟与您创建动画的时间有关,因此,因为所有动画都是在循环中同时创建的,所以它们都具有相同的延迟。

相反,将延迟设置为迭代索引 ( i):

[UIView animateWithDuration:0.3 delay:i options:0 animations:^{ ...

所以第一个将从 0 开始,第二个从 1 开始,第三个从 2 开始,以此类推

(或者(i + 1)如果你喜欢)

于 2015-01-04T15:57:41.647 回答
0

像这样使用延迟:

[UIView animateWithDuration:0.3 delay:i + 1.0 options:0 animations:^{

这会将第一个标签延迟 1 秒,之后的每个标签将延迟 +1 秒

于 2015-01-04T16:00:12.373 回答