NSTimer with multiple time slots in a sequence

Without creating multiple instances NSTimer, how would you achieve NSTimerto run a particular or multiple method with different intervals in the sequence. For example, method 1 (0.3 s), method 2 (0.5), method 3 (0.7), etc.

I would appreciate it if someone could share some sample code.

+5
source share
5 answers

I'm not sure what your ultimate goal is, but after reading your question, I would recommend trying the next path , maybe this is what you were looking for.

, 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];
    // ...
}

, .

[NSObject cancelPreviousPerformRequestsWithTarget:self];
+5

NSTimer , . NSTimer.

+4

i beleive, . 0,3, 1, 0,5 - 2,

0

, 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 selection method according to your requirement.

-1
source

create a timer with a selector with a timer = 0.1 from there in the selection method you can check by saving the static variable float and adding 0.1 to it every time, for example:

static CGFloat counter= 0;

counter+= 0.1;

then check the counter value and call ur methods.

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

All Articles