2011-11-08 10 views
2

私は、複数のアニメーションを持つアプリがあり、シンプルで、オフスクリーンからオンスクリーンに移動してからオフスクリーンに戻るイメージだけです。私はアニメーションブロックを使用しています。コードは次のようになります。objective-c、アニメーションブロックのランダム化

void (^animationOne) (void) = ^{imageOne.center = d;}; 
void (^afterOne) (BOOL) = ^(BOOL f) {imageOne.center = dOrig;}; 
[UIView animateWithDuration:.85 delay:3 options:opts animations:animationOne completion:afterOne]; 

多かれ少なかれ同じフォーマットに従ういくつかのものがあります。しかし、私は彼らがランダムであるように見える順序をしたいと思います。問題の一部は、アニメーションとコンプリートブロックの両方が互いに固有であり、必要なので、アニメーションを配列に貼り付け、シャッフルしてから、アニメーションに使用することはできません。アニメーションブロックを使用してこれをランダム化する方法はありますか?そうでない場合、これを実行して順序をランダム化できる別の方法がありますか?ありがとう。

編集:vikingosegundoからいくつかの有用な提案のおかげで、私はこのように見えるように自分のコードを変更しました:

NSArray *animationsCompletions = [NSArray arrayWithObjects: 
            [NSDictionary dictionaryWithObjectsAndKeys:anim1,@"animation",after1, @"completion", nil], 
            [NSDictionary dictionaryWithObjectsAndKeys:anim2,@"animation",after2, @"completion", nil], 
            [NSDictionary dictionaryWithObjectsAndKeys:anim3,@"animation",after3, @"completion", nil], 
            [NSDictionary dictionaryWithObjectsAndKeys:anim4,@"animation",after4, @"completion", nil], 
            nil]; 

NSDictionary *animationCompletionPair = [animationsCompletions randomElement]; 


[UIView animateWithDuration:.85 delay:3 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]]; 
[UIView animateWithDuration:.85 delay:8 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]]; 
[UIView animateWithDuration:.85 delay:13 options:opts animations:[animationCompletionPair objectForKey:@"animation"] completion:[animationCompletionPair objectForKey:@"completion"]]; 
[UIView animateWithDuration:.85 delay:18 options:opts animations:[animationCompletionPair objectForKey:@"animation"]completion:[animationCompletionPair objectForKey:@"completion"]]; 

しかし、今、私はプログラムを受けていますが、信号を受信:EXC_BAD_ACCESSエラー時に表示してアニメーションはロードしようとしますが、他の情報はコンパイラによって与えられません。任意の提案をいただければ幸いです。

答えて

3

アニメーションブロックと完了ブロックを配列(または2つ)に貼り付けます。配列をシャッフルする代わりに、インデックスをランダムに選択し、対応するアニメーションブロックを実行するだけです。あなたは、各アニメーションは一度だけ実行する場合は、インデックスの二番目の配列を作成して、0からN-1までの番号で初期化

NSArray *animations = [NSArray arrayWithObjects:animation1, animation2, animation3, nil]; 
NSArray *completions = [NSArray arrayWithObjects:completion1, completion2, completion3, nil]; 
int i = arc4random() % 3; 
[UIView animateWithDuration:.85 
         delay:3 
        options:opts 
        animation:[animations objectAtIndex:i] 
       completion:[completions objectAtIndex:i]]; 

2つのアレイを使用して、例えば

、。次に、その配列をシャッフルして、各位置で見つかったインデックスのアニメーションを実行しながら繰り返します。

1

Caleb's answerが正しいですが、NSArrayでcategory methodを使用してランダムな要素を取得することを検討してください。コードの繰り返しを避け、テスト済みのコードを再利用してください。

[array randomElement];は、このようなことができます:

-(id)randomElement 
{ 
    if ([self count] < 1) return nil; 
    NSUInteger randomIndex = arc4random() % [self count]; 
    return [self objectAtIndex:randomIndex]; 
} 

あなたは私のarraytoolsにNSArrayの上のこの実装およびその他の便利なツールがあります。

この場合、配列はアニメーションと補完ブロックの両方で塗りつぶされます。

NSArray *animationsCompletions = [NSArray arrayWithObjects: 
             [NSDictionary dictionaryWithObjectsAndKeys:animation1,@"animation",completion1, @"completion", nil], 
             [NSDictionary dictionaryWithObjectsAndKeys:animation2,@"animation",completion2, @"completion", nil], 
             [NSDictionary dictionaryWithObjectsAndKeys:animation3,@"animation",completion3, @"completion", nil], 
            nil]; 


NSDictionary *animationCompletionPair = [animationsCompletions randomElement]; 

[UIView animateWithDuration:.85 
         delay:3 
        options:opts 
        animation:[animationCompletionPair objectForKey:@"animation"] 
       completion:[animationCompletionPair objectForKey:@"completion"]]; 
+0

大丈夫です、私はこれを試して、動作しているかどうかを報告します。ありがとう! :) – lunadiviner

+0

は私を信頼しています:) – vikingosegundo

+0

もちろん、このコードでアニメーションと補完を接続するには、1つのアニメーションと1つの補完を1つのコレクションで収集し、これを配列に配置する必要があります。 – vikingosegundo