NSTimer 计数

发布于 2024-12-12 20:16:20 字数 303 浏览 3 评论 0原文

我正在尝试创建一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-12-19 20:16:20

以 [NSDate date] 保存计时器启动的时间,然后也记录计时器停止的时间。显示时间将有所不同,按您列出的方式显示。 NSTimer 将仅用于触发更新,并且您可以将间隔设置得相当小,例如 1/10 秒。像这样的东西:

[NSTimer scheduledTimerWithTimeInterval: 0.1f target: self 
    selector: @selector(updateTimeDisplay) userInfo: nil repeats: YES];

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:

[NSTimer scheduledTimerWithTimeInterval: 0.1f target: self 
    selector: @selector(updateTimeDisplay) userInfo: nil repeats: YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文