NSTimer 计数
我正在尝试创建一个从 00:00:00 开始计数到用户按下停止键为止的 NSTimer
。我创建了 1 秒的 NSDateInterval ,但无法让它从 00:00:00 开始计数,只能从当前日期或我设置的日期开始计数。
最好的方法是什么? NSTimer 是执行此操作的最佳方法吗?或者我应该只获取用户按下第一个按钮(设置它的 [NSDate date])的时间与那一秒的时间(由 NSTimer
触发的选择器)?我想确保尽可能少的准确性错误,并且我有时会意识到 NSTimer
的准确性问题。
I am trying to create an NSTimer
that counts up from 00:00:00 to whenever the user presses stop. I have created an NSDateInterval
of 1 second, but I can't get it to count up from 00:00:00, only the current date or a date that I set.
What is the best way to do this? And is NSTimer
the best way to do this or should I just get the difference between the time the user presses the first button ([NSDate date]
to set it) and the time at that second (a selector fired off by NSTimer
)? I want to make sure there is as little accuracy fault as possible, and I am aware of NSTimer
's problems with accuracy sometimes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以 [NSDate date] 保存计时器启动的时间,然后也记录计时器停止的时间。显示时间将有所不同,按您列出的方式显示。 NSTimer 将仅用于触发更新,并且您可以将间隔设置得相当小,例如 1/10 秒。像这样的东西:
Save the time the timer starts with [NSDate date], and then record the time it stops as well. The display time will be the difference, displayed as you listed. The NSTimer will be used just to trigger an update, and you can set the interval fairly small, say 1/10th of a second. Something like: