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