4

以下のコードを 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

4 に答える 4

6

私の意見によると、以下のコードを試してください。

次のように、コントローラーの .h ファイルに NSInteger を 1 つ取ります。

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 回繰り返されます。

それがうまくいくことを願っています。

幸せなコーディング..

于 2012-07-10T08:19:09.880 に答える
5

You could use an NSTimer and within the selector being called by the timer you could invoke the appropriate move method that comes next.

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.

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. 
     }        
}
于 2012-07-10T08:15:36.197 に答える
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++;
      }
于 2012-07-10T09:16:40.587 に答える
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];
}
于 2015-03-23T09:38:47.550 に答える