0

这是我的代码:

float interval = 0.03;

while (interval > 0.005) {
    timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    NSLog(@"%f", interval);
    interval = interval - 0.005;
}

如何使 while 循环在每个循环后等待特定的时间?我一直在寻找诸如 sleep 或 performSelector:withObject:afterDelay 之类的东西,但它们都不起作用,我也不知道如何使用它们……请帮忙?

4

1 回答 1

0

您可以使用递归结构:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    [self recursiveMethode:[NSNumber numberWithDouble:0.3]];

}

-(void)onTimer
{
    NSLog(@"timer");
}


-(void)recursiveMethode:(NSNumber *)interval
{
    timer = [NSTimer scheduledTimerWithTimeInterval:interval.doubleValue target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    NSLog(@"%f", interval.doubleValue);
    double delay = 1;
    [self performSelector:@selector(recursiveMethode:) withObject:[NSNumber numberWithDouble:interval.doubleValue - 0.05] afterDelay:delay];
}
于 2013-03-16T18:05:39.630 回答