xcode 重复自执行选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11409113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repeating self performSelector
提问by Bazinga
I was just wandering If there is a simplier method to repeat the codes below for 20 seconds. If there is, how?
我只是在徘徊如果有更简单的方法来重复下面的代码 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];
回答by Mehul Mistri
According to my opinion Just try this code below,
根据我的意见,试试下面的代码,
Take one NSInteger in your Controller's .h file, like this,
在你的控制器的 .h 文件中取一个 NSInteger,像这样,
NSInteger intTmp;
then in .m file Call NSTimer method like this,
然后在 .m 文件中像这样调用 NSTimer 方法,
[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..
从上面的代码中,testMethod 将调用 20 次,根据您的要求,您的代码将重复 20 次。
Hope It works for you.
希望对你有效。
Happy coding..
快乐编码..
回答by Hermann Klecker
You could use an NSTimer and within the selector being called by the timer you could invoke the appropriate move method that comes next.
您可以使用 NSTimer 并且在计时器调用的选择器中,您可以调用接下来的适当移动方法。
You could use
你可以用
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
userInfo could be used to pass some data to the selector from which the selector can take which move comes next or which move was invoked recently.
userInfo 可用于将一些数据传递给选择器,选择器可以从中获取接下来要进行的移动或最近调用的移动。
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(invokeMove:) userInfo:[NSNumber numberFromInt:1] repeat:NO];
Your selector could 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.
}
}
回答by Ab'initio
Schedule a timer with interval 1 and with in the selector write your code.
安排一个时间间隔为 1 的计时器,并在选择器中编写您的代码。
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++;
}
回答by johndpope
-(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];
}