简体   繁体   English

NSTimer在序列中具有多个时间间隔

[英]NSTimer with multiple time intervals in a sequence

Without creating multiple NSTimer instances, how would one achieve an NSTimer to fire a specific or multiple method with different intervals in a sequence. 在不创建多个NSTimer实例的情况下,如何实现NSTimer以序列中的不同间隔触发特定或多个方法。 For example method1 (0.3 sec), method2 (0.5), method3 (0.7) and so on. 例如方法1(0.3秒),方法2(0.5),方法3(0.7)等。

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. 我不确定您的最终目标是什么,但是在阅读完您的问题后, 我建议您尝试以下方式 ,也许这就是您想要的。

you should put this code where you normally wanted to start the same NSTimer class with different intervals (what is not possible, unfortunately). 您应该将此代码放在通常希望以不同的间隔启动同一NSTimer类的位置(不幸的是,这不可能)。

{
    // ...
    [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 itself does not provide that functionality, it fires either once or repeatedly at fixed intervals. NSTimer本身不提供该功能,而是以固定间隔触发一次或重复触发。 You will require multiple timers to achieve this effect, or move away from NSTimer entirely. 您将需要多个计时器来实现此效果,或者完全远离NSTimer

i beleive you should pass current time interval to the fired selector and further handle it there. 我相信您应该将当前时间间隔传递给触发的选择器,并在那里进行进一步处理。 if time interval is 0.3 you call method1, 0.5 - method2, there's most likely no other way to implement this 如果时间间隔为0.3,则您调用method1,0.5-method2,很可能没有其他方法可以实现此目的

Make a wrapper to wrap the NSTimer method call like this: 创建一个包装器来包装NSTimer方法调用,如下所示:

 - (void) CallTimerWithTimeInterval:(float) interval andSelector:(NSString *)methodName { SEL selector = selectorFromString(methodName); [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(selector) userInfo:nil repeats:YES]; } 

You can call this method and pass the interval and selector method as per your requirement. 您可以调用此方法,并根据需要传递interval和selector方法。

create an timer with selector with timeinterval = 0.1 from there in the selector method, you can check by keeping a static float variable and add 0.1 to it everytime like: 在选择器方法中从那里创建一个带有timeinterval = 0.1的选择器的计时器,您可以通过保留一个静态float变量进行检查,并每次向其添加0.1,例如:

static CGFloat counter= 0;

counter+= 0.1;

then check the counter value and call ur methods.. 然后检查计数器值并调用ur方法。

if(0.3 == counter)
{
    [self callMethod1];
}
else if(0.5 == counter)
{
    [self callMethod2];
}
else if(0.7 == counter)
{
    [self callMethod3];
}
...
...
..
..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM