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

假如想象 提交于 2019-12-23 15:10:39

问题


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?


回答1:


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

}


来源:https://stackoverflow.com/questions/34821629/how-to-make-a-simple-countdown-in-minutes-for-swift-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!