2012-02-06 24 views
0

私はいくつかのビューを持っており、それらを1つずつ表示したいと考えています。私はこれを行う方法を見つけることができません。代わりに、次のコードを使用すると、すべてのビューが一度に表示されます。iosアニメーションを1つずつ

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

すべてshowStar関数は異なる引数を持つライン

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

除いて同一です。 starsUIImageViewの配列です。 ご提案は大歓迎です。

答えて

1

を呼び出します。さまざまなタイマーのtimeIntervalを変更します。

代わりに、1つのNSTimerを繰り返し使用することができます。

NSUInteger count;を.hファイルで宣言してください。

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

あなたの提案を使用しましたが、今度は画面上に1つの画像しか表示されません。 。他は隠れたままです。私は何をすべきか ? – Teo

+0

私は自分の答えを更新しました。 – Ilanchezhian

0

私は再帰で、おそらくこの

- (void)firstAnimation{ 

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

} 

ような何かをするだろうし、私の意見では

+0

okですが、最初の画像を0.5秒で表示してから2秒間停止させたい場合はどうすればよいですか?一時停止をするにはどうすればいいですか? – Teo

+0

animateWithDuration:delay:options:animation:completion:そのアニメーションブロックの代わりに – wattson12

0

実装が間違っているものの、現在のアニメーションのためのフラグがきれいになります。

UIViewUImageViewを置くとよいでしょう。 次に、画像のNSArrayを作成し、imageViewにロードします。

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

その後、実際にあなたが正確に2秒後にすべてのタイマーを発射している

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

すべてのタイマーは同じ時間遅延を使用しているため、すべてが一度に表示されます。また、タイマーで火を呼び出さないで、自動的に発砲します。火を発すると、すぐに火が出るでしょう。

ところで、あなたはちょうどあなたのアニメーションにこれを追加することができ、アニメーションをトリガーするタイマーを使用する必要はありません。

[UIView setAnimationDelay:x]; 

そして、それぞれのビューに対して異なるXを使用しています。

関連する問題