📜  我如何在 swift stackoverflow 中有倒数计时器 - Swift 代码示例

📅  最后修改于: 2022-03-11 15:01:01.104000             🧑  作者: Mango

代码示例1
@IBAction func start(sender: UIButton) {
     self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.update(_:)), userInfo: nil, repeats: true)
     NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
    startTime = NSDate() // new instance variable that you would need to add.
}

func update() {
    let elapsedTime = NSDate().timeIntervalSinceDate(startTime)
    let currTime = totalTime - elapsedTime
    //total time is an instance variable that is the total amount of time in seconds that you want
    countDown.text = String(currTime)
    if currTime < 0 {
        timer.invalidate()
        //do other stuff that you need to do when time runs out.
    }
}