如何在几分钟内为 swift iOS 进行简单的倒计时? [英] How to make a simple countdown in minutes for swift iOS?

查看:45
本文介绍了如何在几分钟内为 swift iOS 进行简单的倒计时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 MINUTES 编写一个简单的计时器倒计时,例如 5 分钟倒计时?

How do I code a simple timer countdown from MINUTES say for example 5 minute countdown?

就像苹果创造的计时器.有没有办法下载和查看苹果应用程序的 xcode 项目?

Like the timer that apple created. Is there a way to download and see the xcode project for apple apps?

推荐答案

将 NSTimer 视为实现此目的的简单方法.可能不是最准确的,但相对简单.NSTimer 每秒调用一次函数更新"(以 1.0 作为第一个参数表示)

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

}

这篇关于如何在几分钟内为 swift iOS 进行简单的倒计时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆