如何从 NSTimer 中找到剩余的时间间隔 [英] How do I find the time interval remaining from NSTimer

查看:80
本文介绍了如何从 NSTimer 中找到剩余的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个 NSTimer.scheduledTimerWithTimeInterval 方法,该方法每 20 分钟间隔一次.我希望能够找出应用程序进入后台模式时还剩下多少时间.我如何知道间隔还剩多少时间?

I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?

谢谢

推荐答案

您可以访问 NSTimer 的 fireDate,它会告诉您计时器何时将再次触发.

You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.

现在和 fireDate 之间的区别是您可以使用 NSDate 的 timeIntervalSinceDate API.

The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.

E.G.类似:

let fireDate = yourTimer.fireDate
let nowDate = NSDate()
let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)

这篇关于如何从 NSTimer 中找到剩余的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆