如何在循环内延迟执行?也许NSTimer?

时间:2011-11-16 14:49:06

标签: iphone ipad

我正在使用for循环,因为我正在使用else if条件。在其中一个else if中,我希望延迟for循环。是否可以在NSTimer中使用continue语句?如果可能,我如何在NSTimer中使用continue语句。

for(int i = 0; i<[array count];i++){

    else if (condition)
    {
    }
    else if (condition)
    {
    }
    else if (condition)
    {
    }
    else if (condition)
    {
       // Here I want to continue the for loop with some delay
    }
    else if (conditions)
    {
    }
    else
    {
    }

}

请任何人帮助我

3 个答案:

答案 0 :(得分:5)

使用NSThread sleepUntilDate:NSDate dateWithTimeIntervalSinceNow:延迟。

示例:

for(int i=0; i<5; i++){

    [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
    NSLog(@"Delaying");

}

输出:

15:57:46.383 Delaying
15:57:47.386 Delaying
15:57:48.388 Delaying
15:57:49.389 Delaying
15:57:50.391 Delaying

答案 1 :(得分:0)

对我来说,你想要暂停线程,而不是使用NSTimer。所以if语句中的内容如下:

[NSThread sleepforTimeInterval:]; 继续;

答案 2 :(得分:0)

您可以简单地使用sleep(&lt; #sleep duration#&gt;);