Las*_*nic -1 iteration objective-c
我想实现一个for循环,这样对于每次迭代,它将等待一秒钟才能进入下一个循环.
for (NSUInteger i = 0; i <=3; i++) {
//...do something
//...wait one second
}
Run Code Online (Sandbox Code Playgroud)
您可以使用dispatch_after以避免在等待时阻塞主线程:
- (void)loopAndWait:(NSUInteger)currentIndex maxIndex:(NSUInteger)maxIndex {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
// do stuff
NSUInteger nextIndex = currentIndex + 1;
if (nextIndex <= maxIndex) {
[self loopAndWait:nextIndex maxIndex:maxIndex];
}
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |