Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
Objective c 恩斯泰默赢得';t在iPad上无效,但在iPhone上可以正常工作_Objective C_Ios_Xcode_Nstimer - Fatal编程技术网

Objective c 恩斯泰默赢得';t在iPad上无效,但在iPhone上可以正常工作

Objective c 恩斯泰默赢得';t在iPad上无效,但在iPhone上可以正常工作,objective-c,ios,xcode,nstimer,Objective C,Ios,Xcode,Nstimer,我有一个按时钟计时的计时器。在时钟运行期间,我调用stopTimer,时钟应该停止。这在iPhone上100%有效,但在iPad上,它有时无法停止计时器。这种情况大约有50%的时间发生。 在stop方法中调用NSLog 这是我的计时器代码: - (void)startSliderTimer { // Get start time [self stopTimer]; startTime = [NSDate timeIntervalSinceReferenceDate] +

我有一个按时钟计时的计时器。在时钟运行期间,我调用stopTimer,时钟应该停止。这在iPhone上100%有效,但在iPad上,它有时无法停止计时器。这种情况大约有50%的时间发生。 在stop方法中调用NSLog

这是我的计时器代码:

- (void)startSliderTimer
{
    // Get start time
    [self stopTimer];
    startTime = [NSDate timeIntervalSinceReferenceDate] + kmaxTimePerSlider;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
}

- (void)updateClock
{
    NSTimeInterval currentTimer = [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval currentTimeLeft = startTime - currentTimer;
    if (currentTimeLeft >= 0) {
        int seconds = currentTimeLeft;
        float milliseconds = currentTimeLeft - seconds;
        int mill = milliseconds * 1000;
        NSString* displayTime = [NSString stringWithFormat: @"%02d:%03d",seconds,mill];
        timerLbl.text = displayTime;
    } else {
        [self tooSlow];
    }
}

- (void) stopTimer
{
    NSLog(@"%s",__FUNCTION__);
    if (self.timer) {
        NSLog(@"Stop Timer");
        [self.timer invalidate];
        self.timer = nil;
    }
}
我在另一个问题/答案中尝试过这样运行计时器作为建议,但它并不总是无效:

self.timer = [NSTimer timerWithTimeInterval:0.01f target:self selector:@selector(updateClock) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];


使用
+timerWith…
方法创建计时器而不进行计划,然后使用
[[nsrunlop currentlunloop]addTimer:timer forMode:NSRunLoopCommonModes]
在所有运行循环上运行它,自己在运行循环上计划计时器。让我知道这是否有效。

创建计时器而不使用
+timerWith…
方法对其进行调度,然后使用
[[nsrunlop currentlunloop]addTimer:timer forMode:NSRunLoopCommonModes]在所有运行循环上运行它。让我知道这是否有效。

对于游戏,也许你应该使用CADisplayLink而不是Ntimer。

对于游戏,也许你应该使用CADisplayLink而不是Ntimer。

你的应用程序在计时器期间有任何滚动吗?这会触发另一种运行循环,这会导致计时器不启动或延迟启动。是的,整个游戏都是基于滚动的!计时器启动正常,因为时钟每0.01秒更新一次。我检查了.isValid属性,它是有效的,因此一定是运行循环问题。为什么这只会在iPad上有所不同?我能做些什么来绕过它(在另一个运行循环中运行计时器?)对不起,它没有滚动,我使用的是UISliders。在计时器运行期间,你的应用程序是否有滚动?这会触发另一种运行循环,这会导致计时器不启动或延迟启动。是的,整个游戏都是基于滚动的!计时器启动正常,因为时钟每0.01秒更新一次。我检查了.isValid属性,它是有效的,因此一定是运行循环问题。为什么这只会在iPad上有所不同?我能做些什么来绕过它(在另一个运行循环中运行计时器?)对不起,它不是滚动的,我使用的是UISliders。在你写这篇文章之前,我已经试过了。我尝试了mainRunLoop和currentRunLoop,但都没有用。计时器运行正常,只是有时候不会停止。确保你的计时器回调看起来像-(void)timerFireMethod:(NSTimer*)计时器。我现在觉得自己太蠢了。计时器没有问题!因为我只倒数了1秒,所以很难注意到,但在开始新游戏之前,我并没有将int重置为0!啊!抱歉,不过还是要谢谢你。我很高兴你明白了:)谢谢,我想这就是当你整天不间断地编写代码时会发生的事情!在你写这之前我就试过了。我尝试了mainRunLoop和currentRunLoop,但都没有用。计时器运行正常,只是有时候不会停止。确保你的计时器回调看起来像-(void)timerFireMethod:(NSTimer*)计时器。我现在觉得自己太蠢了。计时器没有问题!因为我只倒数了1秒,所以很难注意到,但在开始新游戏之前,我并没有将int重置为0!啊!抱歉,不过还是要谢谢你。我很高兴你明白了:)谢谢,我想这就是当你整天不间断地编写代码时会发生的事情!