1

我正在创建一个问答游戏,但我无法找出实现 UIButtons 的最佳方法,这些按钮会以 3 秒的间隔一个接一个地消失。我可以让第一个 UIButton 在 3 秒后消失,但随后的 UIButtons 需要更长的时间。

我相信问题是我的代码变得更加低效,我让每个 UIButton 消失。以下方法是我用重复的 NSInterval 调用的方法,以使每个后续 UIButton 消失:

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

}

4

3 回答 3

3

例如,当您只剩下 2 个按钮时,由于您仍然会生成一个介于 0 和 4 之间的随机数,您实际上只有 20% 的机会让另外一个按钮消失 - 20% 的时间您什么都不做,因为随机数匹配正确的随机数,在这种情况下,60% 的时间你什么都不做,因为它匹配一个已经消失的按钮。

我建议您保留一个最初填充有引用的数组,这 4 个按钮实际上可能会消失(不要费心将正确的数字放在那里,因为无论如何您都不会消失)。在您的函数中,当您在该数组中剩下 N 个按钮时,生成一个介于 0 和 N-1 之间的随机数,这样您就可以使用有效且简洁的代码消失相应的按钮 - 然后(如果消失的按钮不是最后一个数组中的一个)将其与最后一个交换,并将 N 减一。当然,当 N 为 1 时,也不需要随机数。

于 2009-06-08T01:12:04.213 回答
1

Alex Martelli 的回答很好。另一种可能性是用单个按钮数组 button[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];

*** Dan,下面:仍然需要随机化按钮隐藏顺序并确保正确答案未被隐藏的东西,但除此之外,这也是一个很好的解决方案。

于 2009-06-08T18:44:24.810 回答
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]
}
于 2009-06-09T13:20:53.290 回答