How do I code a simple timer countdown from MINUTES say for example 5 minute countdown?
Like the timer that apple created. Is there a way to download and see the xcode project for apple apps?
Look into NSTimer as a simple way to do this. May not be the most accurate but is relatively simple. The NSTimer calls the function "update" every second (indicated by the 1.0 as the first argument)
@IBOutlet var countDownLabel: UILabel!
var count = 300
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
}
func update() {
if(count > 0){
let minutes = String(count / 60)
let seconds = String(count % 60)
countDownLabel.text = minutes + ":" + seconds
count--
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With