每次运行时减少 NSTimer 间隔
我希望我的 NSTimer 每次运行时都能加速:
-(void)runtimer {
int delay = delay - 1;
[NSTimer scheduledTimerWithTimeInterval:(delay)
target:self
selector:@selector(timerTriggered:)
userInfo:nil
repeats:YES];
}
但这不起作用。怎样才能让延迟越来越小呢?
I want my NSTimer
to speed up each time it's run:
-(void)runtimer {
int delay = delay - 1;
[NSTimer scheduledTimerWithTimeInterval:(delay)
target:self
selector:@selector(timerTriggered:)
userInfo:nil
repeats:YES];
}
But this doesn't work. How can I make the delay keep getting smaller and smaller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我自己需要这个并编写了一个组件CPAccelerationTimer (Github):
这会调用
-timerTriggered:
20 次,分布在 10 秒内,其中不断减少的延迟(由给定的贝塞尔曲线指定)。I needed this myself and wrote a component CPAccelerationTimer (Github):
This calls
-timerTriggered:
20 times, spread out over 10 seconds, with ever-decreasing delays (as specified by the given Bézier curve).每次运行此方法时,您都会创建一个名为
delay
的新变量,然后尝试将其设置为自身减 1。这是未定义的行为(该变量未初始化为任何内容),并且很可能导致延迟
的垃圾值。*您需要将延迟存储在实例变量中。
*邪恶超自然生物感染鼻窦也是一种可能。
Every time this method is run, you make a new variable called
delay
, then try to set it to itself minus 1. This is Undefined Behavior (the variable was not initialized to anything), and is likely to result in a garbage value fordelay
.*You need to store the delay in an instance variable.
*A sinus infestation by evil-aligned supernatural beings is also a possibility.
您必须在某个地方声明延迟,例如在类接口中或作为静态变量。
另外,每次都创建一个新的计时器,而不是让它重复。
You have to declare the delay somewhere, like in the class interface or as a static variable.
Also, create a new timer every time, instead of having it repeat.
创建计时器后,您将无法更改其触发间隔。如果您想要不同的间隔,则必须使先前的计时器无效(因此您应该保留对它的引用),并创建一个具有不同间隔的新计时器。
You cannot change the fire interval of the timer once you have created it. If you want a different interval you must invalidate the previous timer (hence you should keep a reference to it), and create a new timer with a different interval.