我怎样才能像音乐播放器一样制作倒数计时器?
How can I make a countdown timer like in a music player?
我有一个标签,它的值为“03:48”。
我想像音乐播放器一样倒计时。我该怎么做?
03:48 03:47 03:46 03:45 ... 00:00
var musictime =3:48
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60)
return String(format: "%02d:%02d", minutes, seconds)
}
func startTimer() {
var duration=musictime.componentsSeparatedByString(":") //split 3 and 48
var count = duration[0].toInt()! * 60 + duration[1].toInt()! //224 second
timerCounter = NSTimeInterval( count )
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
@objc func onTimer(timer:NSTimer!) {
// Here is the string containing the timer
// Update your label here
//println(stringFromTimeInterval(timerCounter))
statusLabel.text=stringFromTimeInterval(timerCounter)
timerCounter!--
}
嗯,通过写代码。这不是“教我如何编程”网站。这是一个您可以 post 就自己编写的代码遇到的具体问题提问的网站。
简而言之,尽管执行以下操作:
记录你的结束时间
let endInterval = NSDate.timeIntervalSinceReferenceDate() + secondsToEnd
创建一个触发 once/second 的重复计时器。
每次触发时,计算 endInterval 剩余的秒数。
计算剩余分钟数为
Int((endInterval-nowInterval)/60)
计算剩余秒数为
Int(endInterval-nowInterval)%60
还有新的 (to iOS 8) class NSDateComponentsFormatter
,我已经阅读了一些但还没有使用过。我相信这会自动为您生成格式化的计时器间隔,例如 hh:mm:ss 。您将使用我上面概述的相同方法,但不是自己计算分钟和秒,而是使用 NSDateComponentsFormatter
.
你应该看看 NSDate 属性 timeIntervalSinceNow. All you need is to set a future date as the endDate using NSDate method dateByAddingTimeInterval 如下:
class ViewController: UIViewController {
@IBOutlet weak var timerLabel: UILabel!
var remainingTime: NSTimeInterval = 0
var endDate: NSDate!
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
remainingTime = 228.0 // choose as many seconds as you want (total time)
endDate = NSDate().dateByAddingTimeInterval(remainingTime) // set your future end date by adding the time for your timer
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateLabel", userInfo: nil, repeats: true) // create a timer to update your label
}
func updateLabel() {
timerLabel.text = endDate.timeIntervalSinceNow.mmss
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// you will need this extension to convert your time interval to a time string
extension NSTimeInterval {
var mmss: String {
return self < 0 ? "00:00" : String(format:"%02d:%02d", Int((self/60.0)%60), Int(self % 60))
}
var hmmss: String {
return String(format:"%d:%02d:%02d", Int(self/3600.0), Int(self / 60.0 % 60), Int(self % 60))
}
}
我有一个标签,它的值为“03:48”。
我想像音乐播放器一样倒计时。我该怎么做?
03:48 03:47 03:46 03:45 ... 00:00
var musictime =3:48
func stringFromTimeInterval(interval: NSTimeInterval) -> String {
let interval = Int(interval)
let seconds = interval % 60
let minutes = (interval / 60)
return String(format: "%02d:%02d", minutes, seconds)
}
func startTimer() {
var duration=musictime.componentsSeparatedByString(":") //split 3 and 48
var count = duration[0].toInt()! * 60 + duration[1].toInt()! //224 second
timerCounter = NSTimeInterval( count )
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
}
@objc func onTimer(timer:NSTimer!) {
// Here is the string containing the timer
// Update your label here
//println(stringFromTimeInterval(timerCounter))
statusLabel.text=stringFromTimeInterval(timerCounter)
timerCounter!--
}
嗯,通过写代码。这不是“教我如何编程”网站。这是一个您可以 post 就自己编写的代码遇到的具体问题提问的网站。
简而言之,尽管执行以下操作:
记录你的结束时间
let endInterval = NSDate.timeIntervalSinceReferenceDate() + secondsToEnd
创建一个触发 once/second 的重复计时器。
每次触发时,计算 endInterval 剩余的秒数。 计算剩余分钟数为
Int((endInterval-nowInterval)/60)
计算剩余秒数为
Int(endInterval-nowInterval)%60
还有新的 (to iOS 8) class NSDateComponentsFormatter
,我已经阅读了一些但还没有使用过。我相信这会自动为您生成格式化的计时器间隔,例如 hh:mm:ss 。您将使用我上面概述的相同方法,但不是自己计算分钟和秒,而是使用 NSDateComponentsFormatter
.
你应该看看 NSDate 属性 timeIntervalSinceNow. All you need is to set a future date as the endDate using NSDate method dateByAddingTimeInterval 如下:
class ViewController: UIViewController {
@IBOutlet weak var timerLabel: UILabel!
var remainingTime: NSTimeInterval = 0
var endDate: NSDate!
var timer = NSTimer()
override func viewDidLoad() {
super.viewDidLoad()
remainingTime = 228.0 // choose as many seconds as you want (total time)
endDate = NSDate().dateByAddingTimeInterval(remainingTime) // set your future end date by adding the time for your timer
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "updateLabel", userInfo: nil, repeats: true) // create a timer to update your label
}
func updateLabel() {
timerLabel.text = endDate.timeIntervalSinceNow.mmss
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// you will need this extension to convert your time interval to a time string
extension NSTimeInterval {
var mmss: String {
return self < 0 ? "00:00" : String(format:"%02d:%02d", Int((self/60.0)%60), Int(self % 60))
}
var hmmss: String {
return String(format:"%d:%02d:%02d", Int(self/3600.0), Int(self / 60.0 % 60), Int(self % 60))
}
}