Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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 当屏幕锁定时,计时器停止_Objective C_Xcode - Fatal编程技术网

Objective c 当屏幕锁定时,计时器停止

Objective c 当屏幕锁定时,计时器停止,objective-c,xcode,Objective C,Xcode,我做了一个计时器,但是如果我锁定屏幕,计时器就会停止。。。当我阻塞时,如何使其工作 -(void)timeRun{ secondsCount = secondsCount - 1; int minuts = secondsCount / 60; int seconds = secondsCount - (minuts * 60); NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d&

我做了一个计时器,但是如果我锁定屏幕,计时器就会停止。。。当我阻塞时,如何使其工作

-(void)timeRun{
    secondsCount = secondsCount - 1;
    int minuts = secondsCount / 60;
    int seconds = secondsCount - (minuts * 60);
    NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minuts, seconds];
    TimerDisplay.text = timerOutput;
    
    
    
    if (secondsCount == 0) {
        [countdownTimer invalidate];
        countdownTimer = nil;
        
        AudioServicesPlaySystemSound(PlaySoundID);
    }
    
}
在这里启动计时器

我把时间定在5分钟


没有更多的上下文很难回答,但您可能需要

UIApplication.sharedApplication.idleTimerDisabled = NO;
或者你需要使用

dispatch_after
或者你需要使用挂钟,只需要使用计时器来更新一些用户界面

你的看起来像后者,即使用挂钟和计时器只是为了更新。如果你准确地倒计时5分钟,你就会有一个开始和结束的时间,并使用一个计时器来不时地更新UI。所以
start
变成了

-(IBAction)Start:(id)sender{
    self.start = NSDate.date.timeIntervalSinceReferenceDate;
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
}
-(void)timeRun{
    secondsCount = (x)( NSDate.date.timeIntervalSinceReferenceDate - self.start + 0.5 ); // Add a bit to get rounding right, depends on your types
    ...
时间运行类似于

-(IBAction)Start:(id)sender{
    self.start = NSDate.date.timeIntervalSinceReferenceDate;
    countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
}
-(void)timeRun{
    secondsCount = (x)( NSDate.date.timeIntervalSinceReferenceDate - self.start + 0.5 ); // Add a bit to get rounding right, depends on your types
    ...
其中
x
将是例如
int
long
取决于
secondscont