swift timer countdown 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: countdown timer using swift StackOverflow
@IBAction func start(sender: UIButton) {
self.timer = Timer.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()
}
}