在Swift中获取Timer中时间间隔的剩余时间

[英]Getting the time remaining in the time interval of a Timer in Swift


I have a sprite "shooting" more sprites every x amount of seconds using a Timer:

我有一个精灵使用计时器每隔x秒“射击”更多精灵:

self.shootingEngine = Timer.scheduledTimer(timeInterval: (Double(60)/Double(self.ratePerMinute)), target: self, selector: #selector(ShootingEnemy.shoot), userInfo: nil, repeats: true)

So the time interval is defined by:

所以时间间隔由下式定义:

(Double(60)/Double(self.ratePerMinute)) // Let's say it is 2 seconds.

The time interval, which in this example would be 2 seconds. It is triggering the shoot() method every 2 seconds. Now, I want to figure out how to get the time remaining until the Timer runs the shoot() method again. For instance, I want to know there are 0.74 seconds remaining until the next shoot()

时间间隔,在此示例中为2秒。它每2秒触发一次shoot()方法。现在,我想弄清楚如何在Timer再次运行shoot()方法之前获得剩余时间。例如,我想知道在下一次拍摄之前还有0.74秒()

I was thinking of using this:

我在考虑使用这个:

NSDate().timeIntervalSince1970 * 1000

And saving the time into a variable and then just finding the difference with the current time. However, I first wanted to check if there is a simpler way to avoid having to store another instance variable.

并将时间保存到变量中,然后只是找出当前时间的差异。但是,我首先要检查是否有更简单的方法来避免必须存储另一个实例变量。

1 个解决方案

#1


1  

Do

let timeRemaining = shootingEngine.fireDate.timeIntervalSince(Date())

Let me know if you have any other questions. Good luck!

如果您有任何其他问题,请与我们联系。祝你好运!


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2017/06/17/35851ad2ca75ea50e4fee9d2d04fb473.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号