2012-02-06 56 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爲每個視圖。