-2

I just made a stopwatch with a tutorial but what I would like to do is to update my 00:00 label as 1 second increasing such as 00:01, 00:02: 00:03 and to do the same for minutes. Is there anyway of doing that? Thanks in advance!

Rob
  • 415,655
  • 72
  • 787
  • 1,044
qursk
  • 23
  • 4
  • 4
    Is there anyway of doing that? Yes. Provide the code you have tried and why it does not work, writing code is what developers do. SO is about helping with code, not providing code. – zaph Aug 14 '15 at 14:03

3 Answers3

2

Then you have to get the date which will start the counting from which is the current date when a particular event occurs, let's say we will start the timer when the view appears, so implement viewWillAppear as follows:

var currentDate = NSDate()

override func viewWillAppear(animated: Bool) {

    currentDate = NSDate()

    var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "updateLabel", userInfo: nil, repeats: true)

    timer.fire()

}

and implement the updateLabel function:

func updateLabel() {

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        var elapsedSeconds: NSTimeInterval = -self.currentDate.timeIntervalSinceNow

        let minutes: Int = Int(elapsedSeconds)/60
        let seconds: Int = Int(elapsedSeconds) - (minutes*60)

        self.timeLabel.text = String(format: "%02d:%02d", minutes, seconds)

    })

}
Firas
  • 1,742
  • 15
  • 25
  • 1
    The OP may be using a timer. See updated comment above WRT difference between two dates. Way to over-thought for a simple question. – zaph Aug 14 '15 at 14:31
  • it is the same, after all you will convert this timeInterval to seconds. – Firas Aug 14 '15 at 14:34
  • Consider that a variable is updated on each timer callback, there is never a date. – zaph Aug 14 '15 at 14:38
  • @zaph yes I tried it for sure, it is working perfectly, please try it and tell me what should I modify? – Firas Aug 14 '15 at 14:42
  • Oops, I did not read the referenced code close enough, it does work. – zaph Aug 14 '15 at 14:51
  • Okay I edited my answer and used the timeIntervalSinceNow, but how do you suggest to not use dates? after all I need a reference date which is the start date right? – Firas Aug 14 '15 at 14:52
0

When formatting time elapsed, NSDateComponentsFormatter is another option:

var start: CFAbsoluteTime!

override func viewDidLoad() {
    super.viewDidLoad()

    start = CFAbsoluteTimeGetCurrent()
    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "handleTimer:", userInfo: nil, repeats: true)
}

lazy var formatter: NSDateComponentsFormatter = {
    let _formatter = NSDateComponentsFormatter()
    _formatter.allowedUnits = .CalendarUnitMinute | .CalendarUnitSecond
    _formatter.zeroFormattingBehavior = .Pad
    return _formatter
}()

func handleTimer(timer: NSTimer) {
    let elapsed = CFAbsoluteTimeGetCurrent() - start
    label.text = formatter.stringFromTimeInterval(elapsed)
}

Admittedly, that will give you the time elapsed in 0:00 format, not 00:00 format.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
-1

This is Objective-C, but you'll get the idea:

-(void) updateTotalTime
{
int forHours = timeInSeconds / 3600, 
remainder = timeInSeconds % 3600, 
forMinutes = remainder / 60, 
forSeconds = remainder % 60;

[elapsedTime setString:[NSString stringWithFormat:NSLocalizedString(@"elapsedTime", nil)
                        ,forHours
                        ,forMinutes
                        ,forSeconds]];

}

and in my Localizable.strings:

"elapsedTime" = "Time: %02d:%02d:%02d";
Chris
  • 2,739
  • 4
  • 29
  • 57
  • 1
    -1 for ObjC, -1 for NSLocalizedString, was not asked for, -100 for just providing code when the OP showed no code or effort. – zaph Aug 14 '15 at 14:26