我是IOS的新手。我想要用户倒数计时器。我的意思是如果它设置计时器5分钟然后5分钟之后一个函数应该调用所以我使用NSTimer,我将在标签中显示min和seconds所以在5分钟之后标签将从0分钟开始,所以当我的应用程序处于前台时它完全正常工作当我的应用程序进入后台时NSTimer将在3分钟后无法工作所以当我要去的时候我是背景我节省时间和NSUserDefault中的第二个,我指的是在前景但是计时器没有完美显示的情况下将这些值分配给标签
-(void) StartTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)timerTick:(NSTimer *)timer
{
timeSec++;
if (timeSec >= 60)
{
timeSec = timeSec-60;
timeMin++;
}
// else
// {
// [self StopTimer];
// }
//Format the string 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
//[timeLabel setStringValue:timeNow];
self.lblForTimer.text= timeNow;
NSNumber *numberValue = [NSNumber numberWithInt:[user.timerInterval intValue]];
if (timeMin>= [numberValue intValue])
{
timeMin = 0;
timeSec++;
}
}
//Call this to stop the timer event(could use as a 'Pause' or 'Reset')
- (void) StopTimer
{
[timer invalidate];
timer=nil;
timeSec = 0;
timeMin = 0;
//Since we reset here, and timerTick won't update your label again, we need to refresh it again.
//Format the string in 00:00
NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d", timeMin, timeSec];
//Display on your label
// [timeLabel setStringValue:timeNow];
self.lblForTimer.text= timeNow;
}

- (void)appWillEnterForeground:(NSNotification *)notification {
NSLog(@"will enter foreground notification");
}
- (void)applicationDidEnterBackground:(NSNotification *)notification {
NSLog(@"did enter backgroun notification");
NSLog(@"%d,%d",timeMin,timeSec);
NSDate *currentDate= [NSDate date];
[[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"backgroundDate"];
[[NSUserDefaults standardUserDefaults] setInteger:timeSec forKey:@"sec"];
[[NSUserDefaults standardUserDefaults] setInteger:timeMin forKey:@"min"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self StopTimer];
}

- (void)appDidBecomeActive:(NSNotification *)notification {
NSDate *dateWhenAppGoesBg= (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"backgroundDate"];
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:dateWhenAppGoesBg];
int timeMinBack=floor(timeSpentInBackground/60);
int timeSecBack=round(timeSpentInBackground - timeMinBack * 60);
NSLog(@"%d%d",timeMinBack,timeSecBack);
// timeMin=(int)[[NSUserDefaults standardUserDefaults] integerForKey:@"min"]+timeMinBack;
timeMin=[user.counterValue intValue];
// timeSec=(int)[[NSUserDefaults standardUserDefaults] integerForKey:@"sec"]+timeSecBack;
if (timeMin>0) {
timeSec=timeSecBack;
}
else
{
timeSec=(int)[[NSUserDefaults standardUserDefaults] integerForKey:@"sec"]+timeSecBack;
}
NSLog(@"timme sec %d",timeSec);
[self StartTimer];
}

这是我执行倒数计时器的代码,它在前景中完美地工作但是当从背景计时器值到forground时非常完美,计时器cananyone之间有1分钟的差距帮助我解决这个问题
答案 0 :(得分:0)
您可以将currentDate保存为属性。在这种情况下,不需要使用NSUserDefaults。
以秒为单位保存和操作超时,并在需要时重新翻译:
NSInteger min = self.timeout / 60;
NSInteger sec = self.timeout % 60;