countdown timer swift code example
Example 1: use timer swift
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(YourController.update),
userInfo: nil,
repeats: true)
@objc func update() {
}
Example 2: how do i have countdown timer in swift stackoverflow
@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()
}
func update() {
let elapsedTime = NSDate().timeIntervalSinceDate(startTime)
let currTime = totalTime - elapsedTime
countDown.text = String(currTime)
if currTime < 0 {
timer.invalidate()
}
}