Pregunta

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.

¿Fue útil?

Solución

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!

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top