Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何用秒表显示准确的时间?_Ios_Swift - Fatal编程技术网

Ios 如何用秒表显示准确的时间?

Ios 如何用秒表显示准确的时间?,ios,swift,Ios,Swift,我最近一直在开发一款秒表应用程序,它与iOS设备上的秒表应用程序类似。我有定时器工作,但我希望它遵循相同的外观和准确的时间,就像苹果的秒表。从现在开始,我只显示秒数。据我所知,必须有某种方法将数字转换为00:00.00格式 import UIKit class ViewController: UIViewController { var counter = 0 var timer = Timer() @IBOutlet weak var timerLbl: UILa

我最近一直在开发一款秒表应用程序,它与iOS设备上的秒表应用程序类似。我有定时器工作,但我希望它遵循相同的外观和准确的时间,就像苹果的秒表。从现在开始,我只显示秒数。据我所知,必须有某种方法将数字转换为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.
    }
}

您应该在创建计时器时创建一个参考日期,然后每次启动计时器时,通过创建日期获取当前时间,然后使用日历获取两个日期之间的时间。你可以得到更多关于如何做到这一点的信息

可能重复的@Wukerplank是用于Objective-C的,我正在用Swift寻找答案。你一定是在开玩笑。查阅NSDateFormatter的文档,并使用所提供答案中的格式字符串。