我最近一直致力于制作一款类似于普通 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.
}
}
最佳答案
你应该在创建定时器的时候创建一个引用日期,然后每次定时器触发时,通过创建日期获取当前时间,然后使用日历获取两个日期之间的时间。您可以获得有关如何执行此操作的更多信息 here
关于ios - 如何用我的秒表显示准确时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45932725/