Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用UIProgressView在倒计时期间处理事件_Iphone_Objective C_Ios_Xcode - Fatal编程技术网

Iphone 使用UIProgressView在倒计时期间处理事件

Iphone 使用UIProgressView在倒计时期间处理事件,iphone,objective-c,ios,xcode,Iphone,Objective C,Ios,Xcode,我希望这不是一个愚蠢的问题,因为我已经在谷歌上搜索并阅读了很多其他类似的问题,但我已经在这个问题上混了好几天了 我已经使用NSTimer和UIProgressView实现了一个倒计时计时器,但我想利用这段时间做更多的事情,即在计时器达到最后10秒时播放警报声 以下是我所做的: -(void) updateTimer { if (time <= 0.0f) { //Invalidate timer when time reaches 0 [tim

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

我已经使用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];
         }
     }

}
以及
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布尔

另外,您正在播放的音频文件是否短于或等于计时器的延迟时间?如果不是,则在结束之前将再次播放

-(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];
        }
    }
}
-(void)updateTimer
{
//如果视图暂停,则停止计数
如果(暂停查看)
{
返回;
}
//完成

如果(time你能展示一下pauseView是如何设置的吗?如果你尝试这样的方法,可能会更简单

你所做的一切似乎都很好,所以我认为问题在于你如何设置pauseView布尔

此外,您正在播放的音频文件是否短于或等于计时器的延迟时间?如果不是,则将在播放结束前再次播放

-(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];
        }
    }
}
-(void)updateTimer
{
//如果视图暂停,则停止计数
如果(暂停查看)
{
返回;
}
//完成

如果(时间正常。实际上,这是我在按下恢复按钮时为了获得计时器恢复而缺少的:

pauseView = nil;

将它添加到
resumeTheGame
方法中,问题就解决了。

好的。实际上,这就是我在按下resume按钮时为了获得计时器resume所缺少的:

pauseView = nil;

将它添加到
resumeTheGame
方法中,问题就解决了。

非常感谢你。我添加了pauseView代码,并且
if(pauseView)
部分工作正常,但是声音仍然没有播放,虽然设置了断点,但该方法似乎被调用了(很多次,当时间达到10并且倒数计时时)。你知道吗?我还需要在暂停查看被取消后恢复时间(调用resumeTheGame方法)。要恢复计时器,您所要做的就是在计时器被取消时将pauseView设置为NO。您必须添加逻辑才能完成此操作。至于音频,它看起来像是将注释发布到控制台,并按预期进入代码块,对吗?似乎剩下的唯一问题是playGameClock1Sound不工作。音频管理什么r您正在使用的是这个吗?项目中是否引用了音频文件?如果是,您可以使用标准的ios方法来播放该文件。仅供参考,您在pauseGame函数中分配和初始化的所有内容都应该在viewDidLoad上执行一次。一旦分配/初始化了所有内容,您就可以显示/隐藏这些内容,而不必记录全部创建。好的……我修复了音频播放问题,但在重新启动游戏后无法重新启动/恢复UIProgressView:
-(void)重新启动游戏{[self-viewDidLoad];[pauseView-removeFromSuperview];choice1Button.userInteractionEnabled=YES;choice2Button.userInteractionEnabled=YES;choice3Button.userInteractionEnabled=YES;choice4Button.userInteractionEnabled=YES;choice5Button.userInteractionEnabled=YES;pauseButton.userInteractionEnabled=YES;[self startCountdown];}
非常感谢。我添加了pauseView代码,if(pauseView)
部分工作正常,但是声音没有继续播放,虽然设置了一个断点,但似乎调用了该方法(很多次,当时间达到10并倒计时时).有什么想法吗?我还需要恢复暂停查看被取消后的时间(调用resumeTheGame方法)。要恢复计时器,您所要做的就是在计时器被取消时将pauseView设置为NO。您必须添加逻辑才能完成此操作。至于音频,它看起来像是将注释发布到控制台,并按预期进入代码块,对吗?似乎剩下的唯一问题是playGameClock1Sound不工作。音频管理什么r您正在使用的是这个吗?项目中是否引用了音频文件?如果是,您可以使用标准的ios方法来播放该文件。仅供参考,您在pauseGame函数中分配和初始化的所有内容都应该在viewDidLoad上执行一次。一旦分配/初始化了所有内容,您就可以显示/隐藏这些内容,而不必记录全部创建。好的……我修复了音频播放问题,但在重新启动游戏后无法重新启动/恢复UIProgressView:
-(void)重新启动游戏{[self-viewDidLoad];[pauseView-removeFromSuperview];choice1Button.userInteractionEnabled=YES;choice2Button.userInteractionEnabled=YES;choice3Button.userInteractionEnabled=YES;choice4Button.userInteractionEnabled=YES;choice5Button.userInteractionEnabled=YES;pauseButton.userInteractionEnabled=YES;[self startCountdown];}