在 Swift 中将计时器标签格式化为 hours:minutes:seconds
Format timer label to hours:minutes:seconds in Swift
我有一个 NSTimer,它从 2 小时开始倒计时到 0。
这是我的一些代码:
var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.5
let timerEnd:NSTimeInterval = 0.0
var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours
// TimeString Function
func timeString(time:NSTimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}
定时器标签是:
TimerLabel.text = "Time: \(timeString(timeCount))"
但是,我的计时器标签显示为:
Time: 200:59.0
如何将我的计时器标签格式化为如下所示:
Time: 01:59:59 // (which is hours:minutes:seconds)?
[请注意,我的倒数计时器没有问题,我只需要知道如何使用 TimeString 函数更改时间格式。]
编辑:
有人提到我的问题可能与这个问题重复:Swift - iOS - Dates and times in different format。但是,我问的是如何使用上面给出的 TimeString 函数更改时间格式。我不是在寻求另一种方法。
例如:
let minutes = Int(time) / 60
给我“200”分钟。等等
你的计算全错了。
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
@rmaddy 的解决方案很准确,回答了问题。但是,问题和解决方案都没有考虑到国际用户。我建议使用 DateComponentsFormatter
并让框架处理计算和格式化。这样做可以让您的代码更不容易出错,并且更适合未来。
我看到这个博客 post,它提供了一个简洁的解决方案:
http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/
从 post 中提取出来的代码片段将替换您当前用于进行计算的代码。 Swift 3 更新:
let duration: TimeInterval = 7200.0
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
let formattedDuration = formatter.string(from: duration)
在 Swift 中实现计时器的最佳方式(swift 4 工作正常)。
声明变量 secs: Int 并分配计时器的值(以秒为单位)。
然后用Timer()函数,一次打折一秒传给这个函数。
var secs = 0
var timer = Timer()
func startTimer(segs: Int) {
seg = segs
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerDiscount), userInfo: nil, repeats: true)
}
func timerDiscount() {
let hours = secs / 3600
let mins = secs / 60 % 60
let secs = secs % 60
let restTime = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
}
声明变量小时、分钟和秒并复制粘贴下面的代码它工作正常。
if counter > 0 {
let hours = counter / 3600
let minutes = counter / 60
let seconds = counter % 60
counter = counter - 1
timerLbl.text = "\(hours):\(minutes):\(seconds)"
}
Swift5
var totalSecond = Int()
var timer:Timer?
根据需求调用startTimer()-
func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
}
@objc func countdown() {
var hours: Int
var minutes: Int
var seconds: Int
if totalSecond == 0 {
timer?.invalidate()
}
totalSecond = totalSecond - 1
hours = totalSecond / 3600
minutes = (totalSecond % 3600) / 60
seconds = (totalSecond % 3600) % 60
timeLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
完成
我有一个 NSTimer,它从 2 小时开始倒计时到 0。
这是我的一些代码:
var timer = NSTimer()
let timeInterval:NSTimeInterval = 0.5
let timerEnd:NSTimeInterval = 0.0
var timeCount:NSTimeInterval = 7200.0 // seconds or 2 hours
// TimeString Function
func timeString(time:NSTimeInterval) -> String {
let minutes = Int(time) / 60
let seconds = time - Double(minutes) * 60
let secondsFraction = seconds - Double(Int(seconds))
return String(format:"%02i:%02i.%01i",minutes,Int(seconds),Int(secondsFraction * 10.0))
}
定时器标签是:
TimerLabel.text = "Time: \(timeString(timeCount))"
但是,我的计时器标签显示为:
Time: 200:59.0
如何将我的计时器标签格式化为如下所示:
Time: 01:59:59 // (which is hours:minutes:seconds)?
[请注意,我的倒数计时器没有问题,我只需要知道如何使用 TimeString 函数更改时间格式。]
编辑: 有人提到我的问题可能与这个问题重复:Swift - iOS - Dates and times in different format。但是,我问的是如何使用上面给出的 TimeString 函数更改时间格式。我不是在寻求另一种方法。
例如:
let minutes = Int(time) / 60
给我“200”分钟。等等
你的计算全错了。
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
@rmaddy 的解决方案很准确,回答了问题。但是,问题和解决方案都没有考虑到国际用户。我建议使用 DateComponentsFormatter
并让框架处理计算和格式化。这样做可以让您的代码更不容易出错,并且更适合未来。
我看到这个博客 post,它提供了一个简洁的解决方案: http://crunchybagel.com/formatting-a-duration-with-nsdatecomponentsformatter/
从 post 中提取出来的代码片段将替换您当前用于进行计算的代码。 Swift 3 更新:
let duration: TimeInterval = 7200.0
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional // Use the appropriate positioning for the current locale
formatter.allowedUnits = [ .hour, .minute, .second ] // Units to display in the formatted string
formatter.zeroFormattingBehavior = [ .pad ] // Pad with zeroes where appropriate for the locale
let formattedDuration = formatter.string(from: duration)
在 Swift 中实现计时器的最佳方式(swift 4 工作正常)。 声明变量 secs: Int 并分配计时器的值(以秒为单位)。 然后用Timer()函数,一次打折一秒传给这个函数。
var secs = 0
var timer = Timer()
func startTimer(segs: Int) {
seg = segs
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerDiscount), userInfo: nil, repeats: true)
}
func timerDiscount() {
let hours = secs / 3600
let mins = secs / 60 % 60
let secs = secs % 60
let restTime = ((hours<10) ? "0" : "") + String(hours) + ":" + ((mins<10) ? "0" : "") + String(mins) + ":" + ((secs<10) ? "0" : "") + String(secs)
}
声明变量小时、分钟和秒并复制粘贴下面的代码它工作正常。
if counter > 0 {
let hours = counter / 3600
let minutes = counter / 60
let seconds = counter % 60
counter = counter - 1
timerLbl.text = "\(hours):\(minutes):\(seconds)"
}
Swift5
var totalSecond = Int()
var timer:Timer?
根据需求调用startTimer()-
func startTimer(){
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(countdown), userInfo: nil, repeats: true)
}
@objc func countdown() {
var hours: Int
var minutes: Int
var seconds: Int
if totalSecond == 0 {
timer?.invalidate()
}
totalSecond = totalSecond - 1
hours = totalSecond / 3600
minutes = (totalSecond % 3600) / 60
seconds = (totalSecond % 3600) % 60
timeLabel.text = String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
完成