【发布时间】: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.
}
}
【问题讨论】:
-
@Wukerplank 用于 Objective - C,我正在 Swift 中寻找答案。
-
你一定是在开玩笑。查找
NSDateFormatter的文档并使用提供的答案中的格式字符串。