问题
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