等待随机时间,然后开始更新 UILabel (iPhone) 中的经过时间

发布于 2024-12-07 14:09:21 字数 1090 浏览 0 评论 0原文

我正在尝试实现一个在随机时间段(0-10 秒之间)后启动计时器的按钮。当计时器运行时,它应该每 0.005 秒更新一次标签,以显示已经过去了多少时间。我遇到的问题有两个:

  1. 我不知道如何让标签每 0.005 秒更新一次经过的时间。

  2. 我无法让应用程序在启动计时器之前等待随机的时间。目前我正在使用 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)如何解决冻结应用程序的“延迟”期

......这将非常有帮助,因为我在这一点上几乎陷入困境。提前致谢。

I'm trying to implement a button that starts a timer after a random period of time (between 0-10s). While the timer is running it should update a label every 0.005s to show how much time has elapsed. The problem i'm having is 2-fold:

  1. I'm not sure how to get the label to update with the elapsed time every 0.005s.

  2. I'm having trouble getting the app to wait the random amount of time before starting timer. At present I'm using sleep(x) however it seems to cause the app to ignore all the other code in the if statement and causes the button image to freeze up (i.e. it looks like its still clicked).

Here is the code I have so far...

- (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
    }
}

If anyone has any suggestions on..

A) How to get the label to update with the elapsed time.

or

B) How to fix the 'delay' period from freezing up the app

... it would be really helpful as I'm pretty much stumped at this point. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我一向站在原地 2024-12-14 14:09:22

您应该使用 NSTimer< /a> 来执行此操作。尝试代码:

- (void)text1; {
  buttonLabel.text = @" ";
}

- (void)text2; {
  buttonLabel.text = @"STOP";
}

- (IBAction)buttonPressed:(id)sender; {
  if ([buttonLabel.text isEqualToString:@"START"]) {
    int startTime = arc4random() % 10; // Find the random period of time to wait
    [NSTimer scheduledTimerWithTimeInterval:(float)startTime target:self selector:@selector(text2:) userInfo:nil repeats:NO];
  }
  else{
    // I put 1.0f by default, but you could use something more complicated if you want.
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(text1:) userInfo:nil repeats:NO];
  }
}

我不太确定您想要如何根据时间更新标签,但是如果您发布更多代码,或者给出一个示例,我将发布有关如何执行此操作的代码,但它只是也使用 NSTimer 。希望有帮助!

You should use an NSTimer to do this. Try the code:

- (void)text1; {
  buttonLabel.text = @" ";
}

- (void)text2; {
  buttonLabel.text = @"STOP";
}

- (IBAction)buttonPressed:(id)sender; {
  if ([buttonLabel.text isEqualToString:@"START"]) {
    int startTime = arc4random() % 10; // Find the random period of time to wait
    [NSTimer scheduledTimerWithTimeInterval:(float)startTime target:self selector:@selector(text2:) userInfo:nil repeats:NO];
  }
  else{
    // I put 1.0f by default, but you could use something more complicated if you want.
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(text1:) userInfo:nil repeats:NO];
  }
}

I'm not exactly sure how you want to update label based on the time, but if you post more code, or give an example, I'll post the code on how to do that, but it would just be using an NSTimer as well. Hope that Helps!

疏忽 2024-12-14 14:09:22

A 的答案可能是:

一旦经过了随机时间(@MSgambel 有一个很好的建议),然后执行:(

timer = [NSTimer scheduledTimerWithTimeInterval:kGranularity target:self selector:@selector(periodicallyUpdateLabel) userInfo:nil repeats:YES];

上面的行可以进入 @MSgambel 的 -text2 方法。)

这将调用 - periodicalUpdateLabel 方法每 kGranularity 秒重复一次。在这种方法中,您可以执行诸如更新标签、检查用户操作或在时间到了或满足其他条件时结束游戏等操作。

这里是 -periodicallyUpdateLabel 方法:

- (void)periodicallyUpdateView {
    counter++;
    timeValueLabel.text = [NSString stringWithFormat:@"%02d", counter];
}

您必须以不同的方式设置文本格式才能获得您想要的内容。另外,使用 kGranularity 将计数器值转换为时间。然而,这就是我发现的,iOS 设备中只有这么多的 cpu 周期。尝试降低到微秒级别会使界面变得缓慢,并且显示的时间开始偏离实际时间。换句话说,您可能必须将标签的更新限制为每百分之一秒或十分之一一次。实验。

The answer to A could be:

Once the random amount of time has passed, (@MSgambel has a good suggestion), then execute:

timer = [NSTimer scheduledTimerWithTimeInterval:kGranularity target:self selector:@selector(periodicallyUpdateLabel) userInfo:nil repeats:YES];

(The above line could go into @MSgambel's -text2 method.)

That will call the -periodicallyUpdateLabel method once every kGranularity seconds, repeatedly. In that method, you could do things like update your label, check for user actions, or end the game if the time is up or some other condition has been met.

And here is the -periodicallyUpdateLabel method:

- (void)periodicallyUpdateView {
    counter++;
    timeValueLabel.text = [NSString stringWithFormat:@"%02d", counter];
}

You'll have to format the text differently to get what you want. Also, translate from the counter value to time using kGranularity. However, and this is what I found, there is only so many cpu cycles in iOS devices. Trying to go down to microsecond level made the interface sluggish and the time displayed started to drift from the actual time. In other words, you may have to limit your updates of the label to once every one hundredth of a second or tenths. Experiment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文