Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.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
第二个iOS上的调用方法_Ios_Objective C_Time_Clock - Fatal编程技术网

第二个iOS上的调用方法

第二个iOS上的调用方法,ios,objective-c,time,clock,Ios,Objective C,Time,Clock,我有一种方法,可以用秒和当前时间显示时钟。这可以很好地工作,只是这段代码会在当前秒的一半或当前秒的四分之三被调用,具体取决于我打开或运行应用程序的时间。该方法通过viewDidLoad方法调用。当这种情况发生时,我的时钟将关闭近1秒。有没有办法在下一秒开始的时候启动我的方法?i、 e.当设备时间为HH:MM:SS.000时启动?注意:如果这与过度使用秒和时钟相混淆,则表示抱歉。我的意思是,我需要在HH:MM:SS.000(设备内部时钟)上启动我的方法,使用: - (id)initWithFire

我有一种方法,可以用秒和当前时间显示时钟。这可以很好地工作,只是这段代码会在当前秒的一半或当前秒的四分之三被调用,具体取决于我打开或运行应用程序的时间。该方法通过
viewDidLoad
方法调用。当这种情况发生时,我的时钟将关闭近1秒。有没有办法在下一秒开始的时候启动我的方法?i、 e.当设备时间为HH:MM:SS.000时启动?注意:如果这与过度使用秒和时钟相混淆,则表示抱歉。我的意思是,我需要在HH:MM:SS.000(设备内部时钟)

上启动我的方法,使用:

- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)seconds 
    target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo 
    repeats:(BOOL)repeats
使用
NSTimer
对象可能是一种方法

添加找到的逻辑,您应该能够在精确的一秒钟内获得正确的结果。(使用此处的逻辑创建分辨率为1秒的NSDate对象,然后在我上面提到的方法中使用该日期)


NSTimer对象不精确。它们取决于经常访问事件循环的应用程序,并且可以变化50毫秒或更长时间(根据我在文档中阅读的内容)。如果我没记错的话,他们会尝试“快速返回”到所需的时间间隔,而不是漂移,但任何给定的射击都不会精确

这就是说,我想我要做的是获取当前NSDate,将其转换为NSTimeInterval,获取上限值(下一个更高的整数),然后启动一个一次性计时器,该计时器将在该时刻启动。然后在该计时器的处理程序中,启动每秒一次的计时器。大概是这样的:

//Get the current date in seconds since there reference date.
NSTimeInterval nowInterval =[NSDate timeInervalSinceReferenceDate];

//Figure out the next even second time interval.
NSTimeInterval nextWholeSecond = ceil(nowInterval);

//Figure out the fractional time between now and the next even second
NSTimeInterval fractionUntilNextSecond = nextWholeSecond - nowInterval;

//Start a one-time timer that will go off at the next whole second.
NSTimer oneTimeTimer = [NSTimer timerWithTimeInterval: fractionUntilNextSecond 
  target: self 
  @selector: (startSecondTimer:) 
  userInfo: nil
  repeats: NO];
以及startSecondTimer方法:

- (void) startSecondTimer: (NSTimer *)timer;
{
  //Start a new, repeating timer that fires once per second, on the second.
  self.secondsTimer = [NSTimer timerWithTimeInterval: 1.0 
  target: self 
  @selector: (handleSecondTimer:) 
  userInfo: nil
  repeats: YES];
} 
您仍然应该在每次调用handleSecondTimer:方法时计算新的时间,而不是依赖于您被调用的次数,因为如果系统在本应调用计时器的时刻变得非常繁忙,而无法找到您,则可能会完全跳过一个调用


免责声明:我还没有尝试过这个,但它应该可以工作。我唯一关心的是边缘案件。例如,当下一整秒钟离现在太近时,一次性计时器的启动速度不够快。在FractionUntinextSecond值中添加一秒可能会更安全,这样秒针开始运行的时间不会超过1秒,而是少于2秒。

您的意思是从HH:MM:SS.000开始,对吗?不是HH:MM:00.000,对吗?谢谢,我注意到它会在非常接近当前秒的时候触发,在0.07秒内,但是仍然会有大约0.1秒的延迟,这意味着我的代码在0.1秒到0.17秒之间触发,这很接近,所以我通过减去大约0.1秒来抵消分数直到下一秒,这非常好!谢谢你的帮助。我总是使用便利类方法来创建计时器,所以我忘记了指定的初始值设定项采用的是第一个启动日期,而不仅仅是间隔时间。好主意。你的方法比我的干净。(投票)我建议在当前日期的时间间隔上使用ceil()函数,而不是使用NSCalendar和date组件。这要简单得多。很好地调用
ceil()
- (void) startSecondTimer: (NSTimer *)timer;
{
  //Start a new, repeating timer that fires once per second, on the second.
  self.secondsTimer = [NSTimer timerWithTimeInterval: 1.0 
  target: self 
  @selector: (handleSecondTimer:) 
  userInfo: nil
  repeats: YES];
}