2009-06-07 12 views
1

私はクイズゲームを作成していますが、3秒間隔で1つずつ消えるUIButtonsを実装する最良の方法を理解できません。 3秒後に最初のUIButtonが消えますが、その後のUIButtonはかなり長くなります。iPhone How To:設定間隔で一気に消えるUIButton

私が問題になるのは、自分のコードが消滅する各UIButtonで非効率になるということです。次のメソッドは、繰り返しのNSIntervalを呼び出して、後続の各UIButtonを消滅させるものです。

while(buttonNum != -1) 
{ 
    buttonNum = rand() % 5; 

    if(buttonNum != [quiz correctNumber]) 
    { 
     if(buttonNum == 0 && [buttonOne isEnabled] == YES) 
     { 
      [UIView beginAnimations:@"buttonFades" context:nil]; 
      [UIView setAnimationDuration:0.5]; 
      [buttonOne setEnabled:NO]; 
      [buttonOne setAlpha:0.0]; 
      [UIView commitAnimations]; 
     } 
     else if(buttonNum == 1 && [buttonTwo isEnabled] == YES) 
     { 
      [UIView beginAnimations:@"buttonFades" context:nil]; 
      [UIView setAnimationDuration:0.5]; 
      [buttonTwo setEnabled:NO]; 
      [buttonTwo setAlpha:0.0]; 
      [UIView commitAnimations]; 
     } 
     else if(buttonNum == 2 && [buttonThree isEnabled] == YES) 
     { 
      [UIView beginAnimations:@"buttonFades" context:nil]; 
      [UIView setAnimationDuration:0.5]; 
      [buttonThree setEnabled:NO]; 
      [buttonThree setAlpha:0.0]; 
      [UIView commitAnimations]; 
     } 
     else if(buttonNum == 3 && [buttonFour isEnabled] == YES) 
     { 
      [UIView beginAnimations:@"buttonFades" context:nil]; 
      [UIView setAnimationDuration:0.5]; 
      [buttonFour setEnabled:NO]; 
      [buttonFour setAlpha:0.0]; 
      [UIView commitAnimations]; 
     } 
     else if(buttonNum == 4 && [buttonFive isEnabled] == YES) 
     { 
      [UIView beginAnimations:@"buttonFades" context:nil]; 
      [UIView setAnimationDuration:0.5]; 
      [buttonFive setEnabled:NO]; 
      [buttonFive setAlpha:0.0]; 
      [UIView commitAnimations]; 
     } 

     buttonNum = -1; 
    } 
} 

}

答えて

3

あなたは左だけ、と言う、2つのボタンを持って、あなたはまだ0と4の間の乱数を生成するので、あなただけ実際には1つの以上のボタンが消え作るの20%のチャンスがあります - - ランダムな数字が正しいものと一致するため、何もしない時間の20%、それらの条件の60%は、すでに消滅したボタンに一致するため何もしません。

実際に消えるかもしれない4つのボタンを最初に入力した配列を維持することをお勧めします(とにかく消えることはないので正しい番号を入れてください)。あなたの関数では、その配列にN個のボタンが残っているときに0からN-1までの乱数を生成するので、効果的で簡潔なコードで適切なボタンを消すことができます - 消滅したボタンが配列内の1つ)は最後のものと交換し、Nを1減らします。もちろん、Nが1の場合は、乱数の必要もありません。

+1

を。ありがとう –

1

Alex Martelliからの正解。別の可能性はこれでボタンの単一のアレイ、ボタン[5]と交換するあなたの大きなループに別々のオブジェクトbuttonOne、buttonTwoなどを置き換えるために、次のようになります。

do { 
    buttonNum = rand() % 5; 
} while (buttonNum == [quiz correctNumber] || ![buttons[buttonNum] isEnabled]); 

[buttons[buttonNum] setEnabled:NO]; // Doesn't need to be in animation block 
[UIView beginAnimations:@"buttonFades" context:nil]; 
[UIView setAnimationDuration:0.5]; 
[buttons[buttonNum] setAlpha:0.0]; 
[UIView commitAnimations]; 

***ダン、以下:何かへボタンを隠す順序をランダム化し、正しい答えが隠されていないことを確認してください。それ以外の場合は素晴らしい解決策です。

1

ボタンが設定された間隔で消える作るのもう一つの方法は、

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay 

方法を使用することです。これを使用するには、それが呼び出されたときにボタンが消えるように関数を定義します:

-(void)hideButton:(UIButton *)button{ 
button.enabled = NO; 
[UIView beginAnimations:@"buttonFades" context:nil]; 
[UIView setAnimationDuration:0.5]; 
[button setAlpha:0.0]; 
[UIView commitAnimations]; 
} 

次に、あなたがこのようなループでは、これらの遅れセレクタ吐き出すだろう:これは素晴らしい仕事

for(int i=0; i<numButtons; i++){ 
    [self performSelector:@selector(mySelector:) withObject:buttons[i] afterDelay:3*i+3] 
}