我正在尝试在iphone应用中的for循环中设置延迟。基本上我会有一个带有一些动作的for循环,我希望每个动作之间有1秒的延迟:
for loop {action 1,delay 1sec,action 2,delay 1sec,action 3,delay 1sec}
我该怎么编码?
答案 0 :(得分:3)
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];
}
答案 1 :(得分:0)
这不涉及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];
}];
希望这有帮助!如果您有疑问,请告诉我。