Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know this is old question, but I want to share my way to achieve this. We have method (timeIntervalSinceDate:) to calculate the interval and have fixed amount of seconds to count down. In my case 300 s.</p> <p><strong>UPDATE JULY, 2015 SWIFT</strong></p> <pre><code>@IBAction func startTimer(sender: UIButton) { sender.selected = !sender.selected; //if selected fire timer, otherwise stop if (sender.selected) { self.timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true); self.startDate = NSDate(); } else { self.stopTimer(); } } func stopTimer() { self.timer.invalidate(); } func updateTimer() { // Create date from the elapsed time var currentDate:NSDate = NSDate(); var timeInterval:NSTimeInterval = currentDate.timeIntervalSinceDate(self.startDate!); //300 seconds count down var timeIntervalCountDown = 300 - timeInterval; var timerDate:NSDate = NSDate(timeIntervalSince1970: timeIntervalCountDown); // Create a date formatter var dateFormatter = NSDateFormatter(); dateFormatter.dateFormat = "mm:ss"; dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0); // Format the elapsed time and set it to the label var timeString = dateFormatter.stringFromDate(timerDate); self.timerLabel?.text = timeString; } </code></pre> <p><a href="https://github.com/gneil90/countdown-label" rel="nofollow noreferrer">Working github swift sample project</a></p> <p><strong>Objective-C</strong></p> <pre><code>- (void)updateTimer { // Create date from the elapsed time NSDate *currentDate = [NSDate date]; NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:self.startDate]; NSLog(@"time interval %f",timeInterval); //300 seconds count down NSTimeInterval timeIntervalCountDown = 300 - timeInterval; NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeIntervalCountDown]; // Create a date formatter NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"mm:ss"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; // Format the elapsed time and set it to the label NSString *timeString = [dateFormatter stringFromDate:timerDate]; self.stopWatch.text = timeString; } - (void)stopTimer { [self.stopWatchTimer invalidate]; self.stopWatchTimer = nil; [self updateTimer]; } - (void)startTimer { if (self.stopWatchTimer) { [self.stopWatchTimer invalidate]; self.stopWatchTimer = nil; } self.startDate = [NSDate date]; // Create the stop watch timer that fires every 100 ms self.stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES]; } </code></pre> <p><strong>UPDATE:</strong></p> <p>it is extremely expensive to create a NSDateFormatter each time you pass through updateTimer. Far better to create the NSDateFormatter outside the loop and reuse it. Credits: @siburb</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload