-1

I would like to take a UIProgressView and link it with an NSTimer (20s) and have the progress bar "countdown" smoothly with the timer. It seems that the information I have found on this isn't quite agreeing with me.

Can anyone tell me how to do this?

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Maegan
  • 273
  • 1
  • 10
  • What part can't you figure out? Making it go backwards? How a timer works? – David Rönnqvist Jul 19 '13 at 14:51
  • Yes, I've tried linking the two unsuccessfully. I know how a timer works, but I am new to the progress view, so I don't really know how to make it go backwards _or_ forward. – Maegan Jul 19 '13 at 14:57
  • You shouldn't use a progress bar for something like a countdown. It's a _progress bar_ not a "countdown bar". Users will be likely confused when it runs backwards. – CouchDeveloper Jul 19 '13 at 15:19
  • @CouchDeveloper They won't be confused if it's part of a quiz. I'm attempting to make it a visual aid to show users that time is running out to answer a question, not show them "backward progress" – Maegan Jul 19 '13 at 15:22

1 Answers1

-1

Have your NSTimer call a function, with

#define TIMER_INTERVAL 0.05f

[NSTimer scheduledTimerWithTimeInterval:TIMER_INTERVAL
    target:self
    selector:@selector(timerMethod:)
    userInfo: [NSNumber numberWithFloat:1.0f]
    repeats:YES];

And then in your function, update your UIProgressView based on the userInfo property of the NSTimer, like so:

-(void) timerMethod: (NSTimer *)timer
{
    float progress = timer.userInfo.floatValue;
    [progressView setProgress:progress animated:YES];

    if (progress <= 0.0f)
        [timer invalidate];
    else
        timer.userInfo = [NSNumber numberWithFloat:(progress - (1.0f/20.0f)*TIMER_INTERVAL)];
}
Pat Lillis
  • 1,152
  • 8
  • 19