增加定时器的时间间隔
可能的重复:
更改计时器的时间间隔
所以我有一个计时器:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];
并且我希望计时器每十秒减少一次,scheduledTimerWithTimeInterval 在十秒后变为 1.5,然后在 10 秒后变为 1.0...是否可以做到这一点,如果可能的话,我该怎么做?
Possible Duplicate:
Change the time interval of a Timer
So I have a timer:
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(createNewImage) userInfo:nil repeats:YES];
And I would like that the timer decrease every ten seconds, that scheduledTimerWithTimeInterval goes to 1.5 after ten seconds then to 1.0 after 10 seconds... Is it possible to do this, and if it is possible, how can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建计时器后就无法对其进行修改。要更改计时器间隔,请使旧计时器无效并使用新间隔创建一个新计时器。
You can't modify a timer after you created it. To change the timer interval, invalidate the old timer and create a new one with the new interval.
您不必使用多个计时器,您所要做的就是添加一个用于时间间隔的变量,然后创建一个使计时器无效、更改变量并再次启动计时器的方法。例如,您可以创建一个启动计时器的方法。
这将是减少计时器间隔并保持计时器运行的简单方法,但我个人更喜欢使用下面的方法,因为它确保计时器只运行一次,这样它就不会重复实例,从而迫使您应用程序变得故障,它也让你的事情变得更容易。
我没有完成所有的编码,但这就是您需要的大部分内容。只需更改一些内容即可开始使用!
You don't have to use several timers, all you have to do is add a variable to use for the time interval, then create a method that invalidates the timer, changes the variable and starts the timer again. For instance you could make a method that starts the timer.
This would be the simple way to decrease the timer interval and keep the timer going, but I would personally prefer using the one below, because it makes sure that the timer has only ran once, that way it will not duplicate the instance, forcing your app to become glitchy and it also makes things easier for you.
I didn't finish all of the coding, but this is most of what you will need. Just change a few things and you'll be good to go!
您将需要一组或一系列计时器,每个计时器对应不同的时间间隔。当您想要更改为时间序列中的下一个时间间隔时,停止/无效一个计时器并启动下一个计时器。
You will need a set or sequence of timers, one for each different time interval. Stop/invalidate one timer and start the next timer when you want to change to the next time interval in your time sequence.