Repeating self performSelector

I just wandered. If there is an easier way to repeat the codes below for 20 seconds. If so, 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]; 
+4
source share
4 answers

In my opinion, just try this code below,

Take one NSInteger in your .h file for example

 NSInteger intTmp; 

then in the .m file call the NSTimer method, for example,

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

And write a 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 the above code, testMethod will ring 20 times, and according to your requirement, your code will repeat 20 times.

Hope this works for you.

Happy coding.

+6
source

You can use NSTimer, and in the selector called by the timer, you can call the corresponding move method, which will be next.

you can use

 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats 

userInfo can be used to transfer some data to a selector, from which a selector can be selected, which step goes next or which move has been called recently.

 NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(invokeMove:) userInfo:[NSNumber numberFromInt:1] repeat:NO]; 

Your selector may be:

 -(void)invokeMove:(id)nextMove { if ([nextMove isKindOfClass: [NSNumber class]]) { int veryNextMove = 0; switch ([nextMove intValue]) { case 1: veryNextMove = 1; case 2: veryNextMove = 2; case 3: veryNextMove = 3; } if (veryNextMove == 0) return; NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval 0.2 target:self selector:@selector(invokeMove:) userInfo:[NSNumber numberFromInt:2] repeat:NO]; //Assuming you use ARC. } } 
+5
source

Schedule a timer at intervals of 1 and use the selector to write down your code.

 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(callMethods) userInfo:nil repeats:YES]; 

selector method

  - (void) callMethods { static int i = 0; if(i < 19) { //your code here for(int j=0; j<2 ;j++) { [self performSelector:@selector( move1) withObject:nil afterDelay:(j*8)/10]; [self performSelector:@selector( move2) withObject:nil afterDelay:((j*8)/10)+0.2]; [self performSelector:@selector( move3) withObject:nil afterDelay:((j*8)/10)+0.4]; } } else { i = 0; [timer invalidate]; } i++; } 
+3
source
 -(void)trickShot{ [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]; [self performSelector:@selector( trickShot) withObject:nil afterDelay:20]; } 
0
source

All Articles