我希望这不是一个愚蠢的问题,因为我已经用谷歌搜索并阅读了很多其他类似的问题,但我已经纠结了这个问题好几天了。
我已经使用NSTimer和UIProgressView实现了倒数计时器,但我希望这次能做更多,即在计时器到达最后10秒时播放警报声。
这就是我所做的:
-(void) updateTimer
{
if (time <= 0.0f)
{
//Invalidate timer when time reaches 0
[timer invalidate];
[[AudioManager sharedManager] muteGameClock1Sound];
[self showGameOverViewController];
}
else
{
time -= 0.1;
[timeProgressView setProgress:time/totalTime animated:YES];
if (time < 10 && pauseView)
{
[[AudioManager sharedManager] playGameClock1Sound];
}
}
}
和viewDidLoad
:
timeProgressView.progress = 1;
time = [ConfigFileManager sharedManager].timePerQuestion.intValue * [DatabaseManager sharedManager].numberOfQuestions;
totalTime = [ConfigFileManager sharedManager].timePerQuestion.intValue * [DatabaseManager sharedManager].numberOfQuestions;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target: self selector: @selector(updateTimer) userInfo: nil repeats: YES];
和pauseGame
方法:
-(IBAction)pauseGame:(id)sender
{
self.view.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.4];
pauseView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 250, 150)];
pauseView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.4];
pauseView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
pauseView.clipsToBounds = YES;
if ([pauseView.layer respondsToSelector: @selector(setCornerRadius:)])
[(id) pauseView.layer setCornerRadius: 10];
resumeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
resumeButton.center = CGPointMake(pauseView.bounds.size.width/4, pauseView.bounds.size.height/2);
[resumeButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[resumeButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[resumeButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageResumeButton] forState:UIControlStateNormal];
[resumeButton addTarget:self action:@selector(resumeTheGame) forControlEvents:UIControlEventTouchUpInside];
restartButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
restartButton.center = CGPointMake(pauseView.bounds.size.width/2, pauseView.bounds.size.height/2);
[restartButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[restartButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[restartButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageReplayButton] forState:UIControlStateNormal];
[restartButton addTarget:self action:@selector(restartTheGame) forControlEvents:UIControlEventTouchUpInside];
quitButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
quitButton.center = CGPointMake(pauseView.bounds.size.width*3/4, pauseView.bounds.size.height/2);
[quitButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[quitButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[quitButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageQuitButton] forState:UIControlStateNormal];
[quitButton addTarget:self action:@selector(showGameOverViewController) forControlEvents:UIControlEventTouchUpInside];
[pauseView addSubview:resumeButton];
[pauseView addSubview:restartButton];
[pauseView addSubview:quitButton];
[self.view addSubview: pauseView];
choice1Button.userInteractionEnabled = NO;
choice2Button.userInteractionEnabled = NO;
choice3Button.userInteractionEnabled = NO;
choice4Button.userInteractionEnabled = NO;
choice5Button.userInteractionEnabled = NO;
pauseButton.userInteractionEnabled = NO;
}
问题是playGameClock1Sound
方法没有以这种方式被调用,但当我说:if (!pauseView && time > 10)
不合理时,然后在这种情况下,当pauseButton是按下,因此pauseView
为TRUE
,再次调用playGameClock1Sound
方法。
如果有人帮我解决这个愚蠢的问题,我将非常感激,如果你告诉我如何处理暂停/恢复,我将非常感激。
答案 0 :(得分:1)
你能说明pauseView是如何设置的吗?如果您尝试这样的事情,可能会更简单...
你正在做的一切似乎都很好所以我认为问题在于你如何设置pauseView bool。
此外,您播放的音频文件是否短于或等于定时器延迟时间?如果不是,它将在完成之前再次播放。
-(void) updateTimer
{
//stop counting if the view is paused
if(pauseView)
{
return;
}
//done
if (time <= 0.0f)
{
//Invalidate timer when time reaches 0
[timer invalidate];
[[AudioManager sharedManager] muteGameClock1Sound];
[self showGameOverViewController];
}
else
{
time -= 0.1;
[timeProgressView setProgress:time/totalTime animated:YES];
if (time < 10)
{
NSLog("Play some audio");
[[AudioManager sharedManager] playGameClock1Sound];
}
}
}
答案 1 :(得分:0)
确定。实际上,这是我在按下继续按钮时恢复计时器所缺少的:
pauseView = nil;
将其添加到resumeTheGame
方法,问题已解决。