I am creating a quiz game and I can't figure out the best way to implement UIButtons that disappear one by one at 3 second intervals. I can get the first UIButton to disappear after 3 seconds, but the subsequent UIButtons take considerably longer.
I believe the problem is that my code becomes more inefficient with each UIButton I make disappear. The following method is what I call with a repeating NSInterval to make each subsequent UIButton disappear:
-(void)hideButton { int buttonNum;
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;
}
}
}