我希望这不是一个愚蠢的问题,因为我已经在Google上搜索并阅读了许多其他类似的问题,但是几天来我一直在处理这个问题。
我已经使用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
方法被再次调用。如果有人帮助我解决这个愚蠢的问题,我将不胜感激,如果您也告诉我如何处理暂停/恢复,我将不胜感激。
最佳答案
您可以显示如何设置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];
}
}
}