我在 swift 3 中创建了一个简单的计时器。
尽管出于某种原因,即使标签显示了太多数字,但它仍然显示了太多的数字 :S 它显示了大量的 9。这与四舍五入有关吗?
谢谢,下面的代码:)
var swiftTimer : Timer?
var swiftCounter: Double = 0.00
@IBOutlet weak var displayTimeLBL: UILabel!
@IBAction func start(_ sender: Any) {
swiftTimer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(ViewController.updateCounter), userInfo: nil, repeats: true)
}
@IBAction func stop(_ sender: Any) {
if swiftTimer != nil{
swiftTimer?.invalidate()
swiftCounter = 0.00
updateLabel()
swiftTimer = nil
}
}
func updateLabel(){
displayTimeLBL.text = String(swiftCounter)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func updateCounter(){
swiftCounter+=0.01
round(swiftCounter)
updateLabel()
}
最佳答案
最简单的解决方案:
func updateCounter(){
swiftCounter+=0.01
round(swiftCounter)
swiftCounter = Double(round(100*swiftCounter)/100)
updateLabel()
}
关于ios - Swift 计时器标签重复出现 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403530/