gpt4 book ai didi

iphone - 使用 UIProgressView 倒计时期间的事件处理

转载 作者:行者123 更新时间:2023-11-29 13:22:08 41 4
gpt4 key购买 nike

我希望这不是一个愚蠢的问题,因为我已经用谷歌搜索并阅读了很多其他类似的问题,但我已经为这个问题纠结了好几天。

我已经使用 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 并且因此 pauseViewTRUE 时,将再次调用 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];
}
}
}

关于iphone - 使用 UIProgressView 倒计时期间的事件处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14123585/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com