显示秒表

发布于 2024-09-07 04:38:48 字数 86 浏览 0 评论 0原文

在我的应用程序中,我想显示秒表之类的东西,即当用户启动应用程序时,顶部手表应该启动,并在用户按下停止按钮时停止。我不确定在这种情况下如何使用 NSTimer。

In my app I want to display stop watch kind of thing i.e when the user starts the app a atop watch should start and stops when the user presses the stop button. I am not sure as to how to use the NSTimer in this case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

人间不值得 2024-09-14 04:38:49

好吧,您知道如何在用户单击按钮时检索开始时间([NSDate date]),因此您只需定期检索当前时间并显示开始时间与实际时间之间的差值即可。用户的当前时间。

NSTimer 用于定期调用方法。在该方法中,您可以使用 [NSDate date] 检索当前时间,然后使用 -[NSDate timeIntervalSinceDate:] 方法来计算自此以来已经过去了多少时间计时器开始计时。 (该方法将以秒为单位返回两个日期之间的差异,精度为亚秒。然后您只需执行一些简单的数学运算即可将原始秒数转换为用户可读的字符串(即 90 秒 => 1 分 30 秒)并将其显示在您的 UI 中。

Well, you know how to retrieve the start time ([NSDate date]) when the user clicks the button, so you just need to periodically retrieve the current time and display the difference between the start time and the current time to the user.

An NSTimer is used to periodically invoke a method. In that method you can just use [NSDate date] to retrieve the current time, then use the -[NSDate timeIntervalSinceDate:] method to figure out how much time has passed since the timer started. (That method will return the difference between the two dates in seconds, with sub-second precision. Then you just need to do some simple math to transform a number of raw seconds into a user-readable string (ie, 90 seconds => 1 min, 30 seconds) and display that in your UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文