问题描述
如果不创建多个NSTimer
实例,那么如何实现NSTimer
以序列中的不同间隔触发特定或多个方法.例如方法1(0.3秒),方法2(0.5),方法3(0.7),依此类推.
Without creating multiple NSTimer
instances, how would one achieve an NSTimer
to fire a specific or multiple method with different intervals in a sequence. For example method1 (0.3 sec), method2 (0.5), method3 (0.7) and so on.
如果有人可以分享任何示例代码,我将不胜感激.
I would appreciate if someone could please share any example code.
推荐答案
我不确定您的最终目标是什么,但是在阅读完您的问题后,我建议您尝试以下方式,也许这就是您想要的.
I'm not sure what your final goal is with this but after reading your question I would recommend to try the following way, maybe this is what you'd look for.
您应该将此代码放在通常希望以不同的时间间隔开始同一NSTimer
类的位置(不幸的是,这是不可能的).
you should put this code where you normally wanted to start the same NSTimer
class with different intervals (what is not possible, unfortunately).
{
// ...
[self performSelector:@selector(method1) withObject:nil afterDelay:0.3f];
[self performSelector:@selector(method2) withObject:nil afterDelay:0.5f];
[self performSelector:@selector(method3) withObject:nil afterDelay:0.7f];
// ...
}
,当需要取消安排所有排队的选择器的时间时,请使用此代码.
and when need to unschedule all those selectors queued, use this code.
[NSObject cancelPreviousPerformRequestsWithTarget:self];
这篇关于NSTimer在一个序列中具有多个时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!