Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating self performSelector

Tags:

xcode

ios

iphone

I was just wandering If there is a simplier method to repeat the codes below for 20 seconds. If there is, how?

[self performSelector:@selector( move1) withObject:nil afterDelay:0.0];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.2];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.4];
[self performSelector:@selector( move1) withObject:nil afterDelay:0.8];
[self performSelector:@selector( move2) withObject:nil afterDelay:0.10];
[self performSelector:@selector( move3) withObject:nil afterDelay:0.12];
like image 795
Bazinga Avatar asked Dec 08 '22 23:12

Bazinga


1 Answers

According to my opinion Just try this code below,

Take one NSInteger in your Controller's .h file, like this,

NSInteger intTmp;

then in .m file Call NSTimer method like this,

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(testMethod:) userInfo:nil repeats:YES];

And write selector like this

-(void)testMethod:(NSTimer *)pTmpTimer
{
    intTmp += 1;

    if(intTmp <= 20)
    {
        [self performSelector:@selector( move1) withObject:nil afterDelay:0.0];
        [self performSelector:@selector( move2) withObject:nil afterDelay:0.2];
        [self performSelector:@selector( move3) withObject:nil afterDelay:0.4];
        [self performSelector:@selector( move1) withObject:nil afterDelay:0.8];
        [self performSelector:@selector( move2) withObject:nil afterDelay:0.10];
        [self performSelector:@selector( move3) withObject:nil afterDelay:0.12];    
    }
    else 
    {
        [pTmpTimer invalidate];
        intTmp = 0;
    }
}

From above code, testMethod will call 20 times and according to your requirement your code will repeat 20 times..

Hope It works for you.

Happy coding..

like image 119
Mehul Mistri Avatar answered Dec 22 '22 18:12

Mehul Mistri