更新计时器 UI 的最佳方法是什么
我的应用程序绘制了一个计时器(详细信息为 0.1 秒),为此我当前使用的是每 0.1 秒触发一次的 NSTime。这感觉绝对是一个糟糕的主意,但我不知道还能怎么做。我并不总是关心 0.1 秒的更新,但我希望它每秒更新一次以上。有没有好的方法可以做到这一点?
My app draws a timer (with detail to .1 seconds), for which I am currently using a NSTime which fires every .1 seconds. This feels like an absolutely terrible idea, but I'm not sure how else to do it. I don't really care about the .1 seconds updating always, but I would like it to update more than once per second. Is there a good way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSTimer
在我看来并不是一个糟糕的方法。NSTimer
通常是一种非常常规的跟踪时间的方法(事实上,在CADisplayLink
出现之前它就被用于动画计时)。除非您发现计时器显示更新的性能无法接受,否则我会坚持使用这种方法。如果您遇到延迟和时间读数不准确的问题,您可以将开始时间存储在
NSDate
中,并继续使用NSTimer
但仅更新显示。在每次计时器事件触发时,您可以通过查找从开始时间到现在的 NSTimeInterval 来更新显示。这样即使出现性能问题,至少在显示时暂时显示应该保持准确。NSTimer
doesn't strike me as a bad approach.NSTimer
is generally a very regular way to keep track of time (indeed it was used for animation timing beforeCADisplayLink
came along). Unless you are seeing unacceptable performance of your timer display updating, I would stick with this approach.If you are having issues with delays and inaccurate time readings, you could store the start time in
NSDate
, and continue to use theNSTimer
but only to update the display. On each timer event firing, you then update the display by finding the NSTimeInterval from the start time to now. This way even if there is a performance issue, at least the time being display should remain accurate at the time of display.