重复自我执行选择器

时间:2012-07-10 08:09:05

标签: iphone ios xcode

我只是想知道是否有一种简单的方法可以重复下面的代码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];

4 个答案:

答案 0 :(得分:6)

根据我的意见,请尝试以下代码,

在Controller的.h文件中加一个NSInteger,就像这样,

NSInteger intTmp;

然后在.m文件中调用NSTimer方法,

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

并像这样写选择器

-(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;
    }
}

从上面的代码中,testMethod将调用20次,并根据您的要求,您的代码将重复20次..

希望它适合你。

快乐编码..

答案 1 :(得分:5)

您可以使用NSTimer,并且在计时器调用的选择器中,您可以调用下一个适当的移动方法。

您可以使用

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

userInfo可以用于将一些数据传递给选择器,选择器可以从中选择接下来的移动或最近调用的移动。

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

你的选择器可能是:

-(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. 
     }        
}

答案 2 :(得分:3)

安排一个间隔为1的计时器,并在选择器中编写代码。

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

选择器方法

    - (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 :(得分:0)

-(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];
}