2011-11-14 2 views
2

의 특정 숫자가 너무 같은 NSTimer를 실행할 수 있습니까? 그래서, 만약 내가 단지 2 초 (.05의 간격으로 가정)을 실행하고 싶다면, 40 번 실행하도록 지정할 수 있습니까? 이것이 가능한가?실행 NSTimer 시간

답변

3

YourViewController.h

NSTimer *f; 
NSInteger counter; 

YourViewController.m는

- (void)setup { 
    // initialization code 
    counter = 0; 
    f = [[NSTimer alloc] initWithFireDate:[NSDate date] 
           interval:.05 
            target:self 
           selector:@selector(checkForCollisions) 
           userInfo:nil repeats:YES]; 

    NSRunLoop *runner = [NSRunLoop currentRunLoop]; 
    [runner addTimer:f forMode: NSDefaultRunLoopMode]; 
} 

- (void)checkForCollisions { 
    // handle check 

    counter++; 
    if (counter >= 40) { 
     [f invalidate]; 
     [f release]; 
    } 
} 
+0

또한, 발사에서 타이머를 지연시킬 수 있습니다 실행 루프 모드에서 인식의 변화합니다. –

0
static int numberOfTimes = 0; 
static int inAndOut = 0; 
... 

int interval = 0.5; 
int 2seconds = 120; 
numberOfTimes = 2seconds/interval; 

- (void) checkForCollisions { 
    if (numberOfTimes <= inAndOut) { 
     //check collisions 
    } 
} 
0

checkForCollisions 메소드에서 카운터를 감소 시키면됩니다.