在 swift 中使用 NSTimer 倒计时
CountDown using NSTimer in swift
我还在学习 swift,我正在尝试设计一个应用程序,在一个部分中,我将这个游戏计时器设置为 7 分钟(将添加弄清楚如何让客户稍后更改),现在是 7:00,我添加了 2 个按钮,
开始,暂停。我想让开始,开始倒计时直到 00:00,然后我打算添加一条弹出消息说时间到了!
到目前为止的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var countDown: UILabel!
var counter = 60
var timer = NSTimer()
func update() {
if counter > 0 {
countDown.text = String(counter--)
}
}
@IBAction func start(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
@IBAction func pause(sender: UIButton) {
// timer.invalidate() stops the whole thing...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
但是,该应用程序一直在崩溃...而且我不知道如何让计时器超过 60 秒,比如如何表示它。
这不是我做倒数计时器的方式。
@IBAction func start(sender: UIButton) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.update(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
startTime = NSDate() // new instance variable that you would need to add.
}
func update() {
let elapsedTime = NSDate().timeIntervalSinceDate(startTime)
let currTime = totalTime - elapsedTime
//total time is an instance variable that is the total amount of time in seconds that you want
countDown.text = String(currTime)
if currTime < 0 {
timer.invalidate()
//do other stuff that you need to do when time runs out.
}
}
还要确保您的 IBAction 插座在界面生成器中是正确的。
我还在学习 swift,我正在尝试设计一个应用程序,在一个部分中,我将这个游戏计时器设置为 7 分钟(将添加弄清楚如何让客户稍后更改),现在是 7:00,我添加了 2 个按钮, 开始,暂停。我想让开始,开始倒计时直到 00:00,然后我打算添加一条弹出消息说时间到了!
到目前为止的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet var countDown: UILabel!
var counter = 60
var timer = NSTimer()
func update() {
if counter > 0 {
countDown.text = String(counter--)
}
}
@IBAction func start(sender: UIButton) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: #selector(ViewController.update), userInfo: nil, repeats: true)
}
@IBAction func pause(sender: UIButton) {
// timer.invalidate() stops the whole thing...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
但是,该应用程序一直在崩溃...而且我不知道如何让计时器超过 60 秒,比如如何表示它。
这不是我做倒数计时器的方式。
@IBAction func start(sender: UIButton) {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.update(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer, forMode: NSRunLoopCommonModes)
startTime = NSDate() // new instance variable that you would need to add.
}
func update() {
let elapsedTime = NSDate().timeIntervalSinceDate(startTime)
let currTime = totalTime - elapsedTime
//total time is an instance variable that is the total amount of time in seconds that you want
countDown.text = String(currTime)
if currTime < 0 {
timer.invalidate()
//do other stuff that you need to do when time runs out.
}
}
还要确保您的 IBAction 插座在界面生成器中是正确的。