Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a simple countdown in minutes for swift iOS? [duplicate]

Tags:

ios

swift

timer

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?

like image 364
newb Avatar asked Jan 15 '16 23:01

newb


1 Answers

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--
    }

}
like image 183
evansjohnson Avatar answered Sep 17 '22 18:09

evansjohnson