0

I've recently been working on making a Stopwatch app that is similar to the one on the stock iOS devices. I have the timer working but I would like it to follow the same look and exact time like the stopwatch Apple has. As of right now I have it just showing the seconds. For what I know there has to be some kind of to convert the numbers to the 00:00.00 format.

import UIKit

class ViewController: UIViewController {

    var counter = 0
    var timer = Timer()

    @IBOutlet weak var timerLbl: UILabel!

    @IBAction func startBtn( sender: Any) {
        timer.invalidate()
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true)

    }

    @IBAction func stopBtn( sender: Any) {
        timer.invalidate()
        counter = 0
    }

    func updateTimer() {
        counter += 1
        timerLbl.text = "(counter)"
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Dexter
  • 23
  • 5
  • Possible duplicate of [What format string do I use for milliseconds in date strings on iPhone?](https://stackoverflow.com/questions/6456626/what-format-string-do-i-use-for-milliseconds-in-date-strings-on-iphone) – Wukerplank Aug 29 '17 at 07:07
  • @Wukerplank that was for Objective - C, I'm looking for an answer in Swift. – Dexter Aug 29 '17 at 07:25
  • You must be kidding. Look up the documentation for `NSDateFormatter` and use the format strings from the provided answer. – Wukerplank Aug 29 '17 at 08:44

1 Answers1

1

You should create a reference date when you create the timer, and then every time the timer fires, get the current time by creating a date, and then get the time between the two dates using a calendar. You can get more information about how to do that here

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45