ios - Swift 计时器标签重复出现 9

标签 ios iphone swift

我在 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()
}

enter image description here

最佳答案

最简单的解决方案:

func updateCounter(){
        swiftCounter+=0.01
        round(swiftCounter)
        swiftCounter = Double(round(100*swiftCounter)/100)
        updateLabel()
    }

关于ios - Swift 计时器标签重复出现 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403530/

相关文章:

ios - 在应用程序中存储静态数据的最佳方式

ios - player2 可以使用 Parse 从设备向 player1 发送通知吗?

ios - 在 IOS 中手动设置日期值并使用日期选择器

ios - 以编程方式使用自动布局添加多个按钮以使用 X、Y、填充约束进行查看

iphone - UIPageViewController 无法正常工作

ios - 收到错误 "Cannot connect to pasteboard server"。 swift

ios - 分配 NSTimer 时不调用 Deinit

macos - 在 OS X Swift 应用程序中显示隐藏窗口

iphone - CADisplayLink 时间戳精度

ios - IBDesignable - 排列通过界面生成器添加的 subview