Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.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 倒数计时器不准确_Iphone_Objective C_Nstimer_Countdowntimer - Fatal编程技术网

Iphone 倒数计时器不准确

Iphone 倒数计时器不准确,iphone,objective-c,nstimer,countdowntimer,Iphone,Objective C,Nstimer,Countdowntimer,我在我的应用程序中倒数到午夜。唯一的问题是倒计时计时器总是关闭5分钟或更长时间。直到午夜,它才真正接近实际剩下的时间。我做了计算,很确定它是正确的,但也许我犯了一个愚蠢的错误 -(void) timerRun { NSDateFormatter *formatterAmPm = [[NSDateFormatter alloc] init]; [formatterAmPm setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];

我在我的应用程序中倒数到午夜。唯一的问题是倒计时计时器总是关闭5分钟或更长时间。直到午夜,它才真正接近实际剩下的时间。我做了计算,很确定它是正确的,但也许我犯了一个愚蠢的错误

-(void) timerRun
{
NSDateFormatter *formatterAmPm = [[NSDateFormatter alloc] init];
[formatterAmPm setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterAmPm setDateFormat:@"a"];

NSString *amPmString = [formatterAmPm stringFromDate:[NSDate date]];

secondsCount = secondsCount + 1;
int hours = ((12*3600)-(secondsCount))/3600;
int minutes = ((12*3600)-(secondsCount)-(hours*3600))/60;
int seconds = ((12*3600)-(secondsCount)-(hours*3600))%60;

NSString *timerOutput = [NSString stringWithFormat:@"%.2d:%.2d:%.2d", hours, minutes, seconds];
_countDownLabel.text = timerOutput;

if ((secondsCount == 0) && ([amPmString isEqualToString:@"AM"]))
{
    [_countdownTimer invalidate];
    _countdownTimer = nil;
    MainViewController *ViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    ViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self.navigationController pushViewController:ViewController animated:YES];
    self.sidePanelController.centerPanel = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];

}
}

-(void) setTimer
{
NSDateFormatter *formatterHour = [[NSDateFormatter alloc] init];
[formatterHour setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterHour setDateFormat:@"HH"];

NSDateFormatter *formatterMinute = [[NSDateFormatter alloc] init];
[formatterMinute setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterMinute setDateFormat:@"mm"];

NSDateFormatter *formatterSecond = [[NSDateFormatter alloc] init];
[formatterSecond setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterSecond setDateFormat:@"ss"];

NSDateFormatter *formatterAmPm = [[NSDateFormatter alloc] init];
[formatterAmPm setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
[formatterAmPm setDateFormat:@"a"];

NSString *amPmString = [formatterAmPm stringFromDate:[NSDate date]];
NSString *hourString = [formatterHour stringFromDate:[NSDate date]];
NSString *minuteString = [formatterHour stringFromDate:[NSDate date]];
NSString *secondString = [formatterHour stringFromDate:[NSDate date]];

int hour = (([hourString intValue]) * 60 * 60);
int minute = (([minuteString intValue]) * 60);
int second = [secondString intValue];
if ([amPmString isEqualToString:@"PM"])
{
    stopTime = 12*60*60;
}
else
{
    stopTime = 24*60*60;
}

secondsCount = hour + minute + second;

secondsCount = secondsCount - stopTime;

_countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self     selector:@selector(timerRun) userInfo:nil repeats:YES];
}

NSTimer
不精确,它会在适当的时间发射。这意味着您不能在尝试时使用它来计算秒数


相反,您需要做的是在
timerRun
方法中获取实际时间,并使用该时间确定剩余的秒数。您已经在调用
[NSDate]
来获取AM/PM指示,只需使用它来获取当前时间,然后计算剩余的秒数。

n计时器不是跟踪时间的准确方法,因为它们与运行循环有关。