我试图实现一个按钮,在随机时间段(0-10s之间)后启动一个定时器。计时器运行时,它应该每0.005秒更新一次标签以显示已经过了多长时间。我遇到的问题是2倍:等待随机时间,然后开始更新UILabel(iPhone)中的流逝时间
我不知道如何让标签更新与经过的时间每0.005秒。
我无法让应用程序在启动计时器之前等待随机时间。目前我正在使用
sleep(x)
,但它似乎会导致应用忽略if
语句中的所有其他代码,并导致按钮图像冻结(即看起来像仍然点击了它)。
这里是我到目前为止的代码...
- (IBAction)buttonPressed:(id)sender
{
if ([buttonLabel.text isEqualToString:@"START"])
{
buttonLabel.text = @" "; // Clear the label
int startTime = arc4random() % 10; // Find the random period of time to wait
sleep(startTime); // Wait that period of time
startTime = CACurrentMediaTime(); // Set the start time
buttonLabel.text = @"STOP"; // Update the label
}
else
{
buttonLabel.text = @" ";
double stopTime = CACurrentMediaTime(); // Get the stop time
double timeTaken = stopTime - startTime; // Work out the period of time elapsed
}
}
如果任何人有..
A)任何建议,如何获得标签与经过时间更新。
或
B)如何修复冻结了应用
的“延迟”时期......因为我在这一点上几乎难倒这将是非常有益的。提前致谢。
sleep()阻止执行 – EricLeaf