0
我正在用提升按钮制作游戏。当然,只要将它保持为启用状态,就可以让玩家不断地点击它。所以,我需要20秒的延迟时间才能再次按下按钮。另外,是否有可能在按钮上显示此进程,最好是按钮本身?增加按钮延迟
我正在用提升按钮制作游戏。当然,只要将它保持为启用状态,就可以让玩家不断地点击它。所以,我需要20秒的延迟时间才能再次按下按钮。另外,是否有可能在按钮上显示此进程,最好是按钮本身?增加按钮延迟
未来,请尝试展示您试图解决您的问题。然而,既然你是新人,我会让它滑过一次!
此代码使用每秒调用一次的NSTimer
。它会触发您为“Boost”指定的任何代码。它也会禁用用户在你的按钮上的交互,直到从按钮被按下20秒后,用户再次按下按钮,最后,这段代码显示剩余的秒数,直到“Boost”可以再次使用您的按钮的titleLabel
属性。
- (IBAction)buttonPressed:(id)sender
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(activateBoost) userInfo:nil repeats:YES];
}
- (void)activateBoost
{
if (myButton.userInteractionEnabled == YES) {
//Put your Boost code here!
}
if ([myButton.titleLabel.text intValue] == 0) {
[myTimer invalidate];
[myButton setTitle:@"20" forState:UIControlStateNormal];
myButton.userInteractionEnabled = YES;
}else{
myButton.userInteractionEnabled = NO;
int currentTime = [myButton.titleLabel.text intValue];
int newTime = currentTime - 1;
myButton.titleLabel.text = [NSString stringWithFormat:@"%d",newTime];
}
}
为了让上面的代码工作,你需要申报一个NSTimer
名为“myTimer”和UIButton
“myButton的”。您还需要将按钮的初始文本设置为“20”。
我的回答对你有帮助吗? – 2012-07-26 03:52:06