在 Swift 中格式化秒表的时间

Formatting stopwatch's time in Swift

我正在尝试用秒表解决我的问题。每 10 秒在他结束时不为零,在 60 秒后不为 + 1 分钟。时间格式应为 m:ss (0:00)。开始和停止是我应用程序中的相同按钮。你能帮帮我吗?

var timer: Timer?
var isStarted = false
var counter = 0.00

@objc func updateTimeLabel() {
        counter += 0.01
        timerLabel.text = String(round(counter*1000)/1000)
      }


@IBAction func startStopButtonDidTouch(_ sender: UIButton) {
        if isStarted { //When tapped STOP
            timer?.invalidate()
            isStarted = false
            locationManager.stopUpdatingLocation()
            startStopButton.setTitle("START", for: .normal)
       
      } else { //When tapped START
        locationManager.startUpdatingLocation()
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTimeLabel), userInfo: nil, repeats: true)
        isStarted = true
        startStopButton.setTitle("STOP", for: .normal)
       
        }
  }

有一个日期格式化程序可以将时间间隔显示为日期组件

var counter : TimeInterval = 0

let dateFormatter : DateComponentsFormatter = {
    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.minute, .second]
    formatter.zeroFormattingBehavior = .pad
    return formatter
}()

@objc func updateTimeLabel() {
    counter += 1
    timerLabel.text = dateFormatter.string(from: counter)!
}