我正在尝试实现一个在随机时间段(0-10 秒之间)后启动计时器的按钮。当计时器运行时,它应该每 0.005 秒更新一个标签以显示已经过去了多少时间。我遇到的问题是 2 倍:
我不确定如何让标签每 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)如何解决冻结应用程序的“延迟”期
......这真的很有帮助,因为我在这一点上非常难过。提前致谢。