iPhone计时器 - 如何等待几秒钟?

时间:2011-06-24 09:09:19

标签: iphone timer nstimer

我想等几秒钟......

 for (int i = 0; i < 3; i++) {
    // something A
    ...

    // wating
    [NSTimer scheduledTimerWithTimeInterval:10
                                     target:self
                                   selector:@selector(endTimer:)
                                   userInfo:nil 
                                    repeats:YES];

    // something B

    ...
} 


- (void)endTimer:(NSTimer *)timer {
    NSLog(@"end timer");
    [timer invalidate];
}

我想'A - &gt;等待 - &gt; B'

但不要等待......

A - &gt; B - &gt; A - &gt; B - &gt; A - &gt; B - &gt;结束计时器,结束计时器,结束计时器

有什么办法吗?

美好的一天

2 个答案:

答案 0 :(得分:1)

试试这个,

-(void)callA{
   //Do your thing...
}
-(void)callB{
    //Do your thing...
}
-(void)callFunction{
   count++;
   if(count<3){
     [self performSelector:@select(callA) withObject:nil];
     [NSThread sleepForTimeInterval:3.0];
     [self callB];
   }
   else{
      [timer invalidate];
   }
}

现在,在main函数中创建要调用上述函数的计时器。

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

答案 1 :(得分:0)

即使你的问题措辞不好,

// set myCount somewhere as an INT with value 0
// int myCount = 0;

-(void)callA
{
    if (myCount < 3){
        [self performSelector:@selector(callB) withObject:nil afterDelay:1.0];
    }
}


-(void)callB
{
    myCount +=1;
    [self performSelector:@selector(callA) withObject:nil afterDelay:1.0];

}