我的For Loop出了什么问题

时间:2016-02-11 11:06:58

标签: ios objective-c

这是我的For Loop。它生成我的按钮图像并显示它们。

没有我的延迟,一切正常。它会立即显示。但现在我想延迟循环,以便动画不会同时启动。

我看到的是,NSLogs按预期工作,NSLogs会延迟显示NSLogs

但是当所有double secondsToSleepForLoop = .05; for(int startingSquares = 0; startingSquares < _revealedSquares; startingSquares++) { int x = arc4random_uniform(5); int y = arc4random_uniform(5); if(buttons[x][y] == 0) { [NSThread sleepForTimeInterval:secondsToSleepForLoop]; NSLog(@"ADD"); NSLog(@"x: %d", x); NSLog(@"y: %d", y); //ROW 1 if (x == 0 && y == 0) { [UIView transitionWithView:_cell00 duration:.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [_cell00 setImage: [UIImage imageNamed:[NSString stringWithFormat:@"Dot_%d.png", buttons[0][0]]] forState:UIControlStateNormal]; } completion:^(BOOL finished) { }]; } if (x == 1 && y == 0) { [UIView transitionWithView:_cell10 duration:.3 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{ [_cell10 setImage: [UIImage imageNamed:[NSString stringWithFormat:@"Dot_%d.png", buttons[1][0]]] forState:UIControlStateNormal]; } completion:^(BOOL finished) { }]; and so on.... } 完成时,会显示图像。一切都在同一时间。 如果我有25个延迟0.05的按钮,所有图像都会在1,25秒后显示。

但我想在每0.05秒后显示一张图片。

我在做错了什么?

<div id="text"></div>
<h2 class="form-sign">
<b>DATA</b>
</h2>
<br></br>
<div>
<p>
<b>Film: </b>
POSIN
</p>
<p>
<b>Topic: </b>
HORROR
</p>
</div>
<input name="Description" value="1" type="hidden">

1 个答案:

答案 0 :(得分:3)

在当前的runloop循环结束之前,动画才会开始。你的睡眠只是延迟了runloop循环的结束。我认为你应该尝试dispatch_after()而是随机延迟。

此外,对于任何原因,永远不要在主线程上休眠。

相关问题