2012-02-06 57 views
0

我有一些意见,我想逐个显示它们。我找不到如何做到这一点。相反,如果我使用下面的代码,所有视图都会一次显示。ios动画一个接一个

NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showStar2) userInfo:nil repeats:NO]; 
     NSTimer *timer2 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showStar3) userInfo:nil repeats:NO]; 
     NSTimer *timer3 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showStar4) userInfo:nil repeats:NO]; 
     NSTimer *timer4 = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showStar5) userInfo:nil repeats:NO]; 

     [timer1 fire]; 
     [timer2 fire]; 
     [timer3 fire]; 
     [timer4 fire]; 


-(void)showStar2 
{ 

    [UIView beginAnimations:@"anim1" context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [stars[1] setAlpha:1]; 
    [UIView commitAnimations]; 
} 

所有展星功能都是相同的,除了线

[UIView beginAnimations:@"anim1" context:NULL]; 
[stars[1] setAlpha:1]; 

其具有不同的参数。 starsUIImageView的数组。 欢迎任何建议。

回答

1

其实你2秒后准确地发射所有的计时器。更改不同定时器的timeInterval。

取而代之,您可以使用单个NSTimer,它将重复触发。

在.h文件中声明NSUInteger count;

启动的NSTimer如下:

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showStar:) userInfo:nil repeats:YES]; 

和你showStar方法应该如下:

-(void)showStar:(NSTimer*)timer 
{ 
    if(count > 0) 
    { 
     [stars[count-1] setAlpha:0]; 
    } 
    [UIView beginAnimations:@"anim1" context:NULL]; 
    [UIView setAnimationDuration:0.5]; 
    [stars[count] setAlpha:1]; 
    [UIView commitAnimations]; 

    count++; 

    if(count == 4) 
    { 
     [timer invalidate]; 
     count = 0; 
    } 
} 

下面的代码已被添加。

if(count > 0) 
{ 
    [stars[count-1] setAlpha:0]; 
} 
+0

我已经使用了你的建议,现在屏幕上只显示一个图像,其他人保持隐藏状态。我该怎么办 ? – Teo 2012-02-06 11:00:00

+0

我已经更新了我的答案。 – Ilanchezhian 2012-02-06 11:52:29

0

我会做这样的事情

- (void)firstAnimation{ 

    [UIView animateWithDuration:2.0f animations:^{ 
     //first animation 
    } completion:^(BOOL finished){ 
     [self secondAnimation]; 
    }]; 

} 
递归

可能和当前的动画标志将清洁剂虽然

+0

好的,但如果我想让第一张图片在0.5秒内出现,然后暂停2秒,该怎么办?我该如何暂停?我不希望所有的图像一个接一个地出现而没有停顿 – Teo 2012-02-06 11:09:32

+0

使用animateWithDuration:delay:options:animations:completion:而不是那个动画块 – wattson12 2012-02-06 11:40:31

0

在我看来你的实现是错误的。

最好将UImageView放在您的UIView上。 接下来创建一个NSArray的图像,然后加载到imageView上。

arrAnimations = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"image1.png],.....nil]; 

然后调用

imageView.animationImages= arrAnimations; 
imageView.animationDuration=2; 
[imageView startAnimating]; 
1

所有的定时器都使用相同的时间延迟,所以这就是所有的一次出现。另外,不要在定时器上点火,它们会自动启动。打电话可能会让他们马上起火。

顺便说一句,你不需要使用定时器触发动画,你可以添加到您的动画:

[UIView setAnimationDelay:x]; 

,并使用不同的x为每个视图。