【问题标题】:How to show the exact time with my Stopwatch?如何使用秒表显示准确时间?
【发布时间】:2017-08-29 07:03:11
【问题描述】:

我最近一直致力于制作一款类似于现有 iOS 设备上的秒表应用。我有计时器工作,但我希望它遵循与 Apple 秒表相同的外观和准确时间。截至目前,我只显示秒数。据我所知,必须有某种将数字转换为 00:00.00 格式的方法。

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.
    }
}

【问题讨论】:

标签: ios swift


【解决方案1】:

您应该在创建计时器时创建一个参考日期,然后每次计时器触发时,通过创建日期获取当前时间,然后使用日历获取两个日期之间的时间。您可以获取有关如何执行此操作的更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    相关资源
    最近更新 更多