我试图在iphone应用程序中的循环内设置延迟。基本上我会有一个for循环与几个动作,我想每个动作之间1秒延迟:延迟在for循环 - iPhone
for循环{动作1,延迟1秒,动作2,延迟1秒,动作3,延迟1秒}
我该如何编码?
我试图在iphone应用程序中的循环内设置延迟。基本上我会有一个for循环与几个动作,我想每个动作之间1秒延迟:延迟在for循环 - iPhone
for循环{动作1,延迟1秒,动作2,延迟1秒,动作3,延迟1秒}
我该如何编码?
for (loop) {
[self action1];
[self performSelector:@selector(action2) withObject:nil afterDelay:1.0];
[self performSelector:@selector(action3) withObject:nil afterDelay:1.0];
}
希望这是你在找什么!
编辑
试试这个。它会完成了当前运行的方法,并移动到下一个。
for (loop) {
[self performSelectorOnMainThread:@selector(action1) withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(action2) withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(action3) withObject:nil waitUntilDone:YES];
}
我设法在调用(void)action2之前得到一个延迟,但是它不会返回for循环,然后延迟并调用(void)action3。如何在完成(void)action2等之后返回for循环? – user1716811
更新!尝试这个!!或者,如果你想使用睡眠(),请检查这个http://stackoverflow.com/questions/2956175/when-i-really-need-to-use-nsthread-sleepfortimeinterval1 – Nina
这不涉及for循环,但将采取行动列表并执行它们以延长时间。
NSArray *selectorStrings = @[ @"action1", @"action2", @"action3" ];
[selectorStrings enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SEL selector = NSSelectorFromString((NSString *)obj);
NSTimeInterval delay = (NSTimeInterval)idx;
[self performSelector:selector withObjet:nil afterDelay:delay];
}];
希望这有助于!如果您有任何问题,请告诉我。
使用语句sleep(1); –
可能你想使用'[NSThread sleepForTimeInterval:(NSTimeInterval)]'? NSTimeInterval以秒为单位。 – DanSkeel