在 Swift 和 SwiftUI 中使用计时器格式化毫秒
Formatting milliseconds with a Timer in Swift and SwiftUI
我有这个函数可以成功地将计数器变量转换为格式精美的分钟和秒字符串。如何将其扩展为显示 MM:SS:milliseconds 的格式?
我想也许可以做 let milliseconds = Int(Double(seconds / 60) * 100)
并将其附加到 return。有什么建议吗?
注意:我的计时器应该以足够的粒度发布 @State var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
func formatMmSs(counter: Double) -> String {
let minutes = Int(counter) / 60 % 60
let seconds = Int(counter) % 60
return String(format: "%02i:%02i", minutes, seconds)
}
您只需将计数器(秒数)乘以 1000,
截断除以 1000 的余数并将结果强制为整数:
func formatMmSsMl(counter: Double) -> String {
let minutes = Int((counter/60).truncatingRemainder(dividingBy: 60))
let seconds = Int(counter.truncatingRemainder(dividingBy: 60))
let milliseconds = Int((counter*1000).truncatingRemainder(dividingBy: 1000))
return String(format: "%02d:%02d.%03d", minutes, seconds, milliseconds)
}
或作为 BinaryFloatingPoint 类型的计算 属性 实现:
extension BinaryFloatingPoint {
var intValue: Int { Int(self) }
var minutesSecondsMilliseconds: String {
String(format: "%02d:%02d.%03d",
(self / 60).truncatingRemainder(dividingBy: 60).intValue,
truncatingRemainder(dividingBy: 60).intValue,
(self * 1000).truncatingRemainder(dividingBy: 1000).intValue)
}
}
您还可以自定义格式化浮点数。无需强制转换为整数,如下所示:
extension TimeInterval {
var minutesSecondsMilliseconds: String {
String(format: "%02.0f:%02.0f.%03.0f",
(self / 60).truncatingRemainder(dividingBy: 60),
truncatingRemainder(dividingBy: 60),
(self * 1000).truncatingRemainder(dividingBy: 1000).rounded(.down))
}
}
123.45678.minutesSecondsMilliseconds // "02:03.456"
这是继续使用您的方法,也遵循 Leo 的方法,
func formatMmSs(counter: Double) -> String {
let minutes = Int(counter) / 60 % 60
let seconds = Int(counter) % 60
let milliseconds = Int(counter*1000) % 1000
return String(format: "%02d:%02d:%03d", minutes, seconds, milliseconds)
}
我有这个函数可以成功地将计数器变量转换为格式精美的分钟和秒字符串。如何将其扩展为显示 MM:SS:milliseconds 的格式?
我想也许可以做 let milliseconds = Int(Double(seconds / 60) * 100)
并将其附加到 return。有什么建议吗?
注意:我的计时器应该以足够的粒度发布 @State var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
func formatMmSs(counter: Double) -> String {
let minutes = Int(counter) / 60 % 60
let seconds = Int(counter) % 60
return String(format: "%02i:%02i", minutes, seconds)
}
您只需将计数器(秒数)乘以 1000, 截断除以 1000 的余数并将结果强制为整数:
func formatMmSsMl(counter: Double) -> String {
let minutes = Int((counter/60).truncatingRemainder(dividingBy: 60))
let seconds = Int(counter.truncatingRemainder(dividingBy: 60))
let milliseconds = Int((counter*1000).truncatingRemainder(dividingBy: 1000))
return String(format: "%02d:%02d.%03d", minutes, seconds, milliseconds)
}
或作为 BinaryFloatingPoint 类型的计算 属性 实现:
extension BinaryFloatingPoint {
var intValue: Int { Int(self) }
var minutesSecondsMilliseconds: String {
String(format: "%02d:%02d.%03d",
(self / 60).truncatingRemainder(dividingBy: 60).intValue,
truncatingRemainder(dividingBy: 60).intValue,
(self * 1000).truncatingRemainder(dividingBy: 1000).intValue)
}
}
您还可以自定义格式化浮点数。无需强制转换为整数,如下所示:
extension TimeInterval {
var minutesSecondsMilliseconds: String {
String(format: "%02.0f:%02.0f.%03.0f",
(self / 60).truncatingRemainder(dividingBy: 60),
truncatingRemainder(dividingBy: 60),
(self * 1000).truncatingRemainder(dividingBy: 1000).rounded(.down))
}
}
123.45678.minutesSecondsMilliseconds // "02:03.456"
这是继续使用您的方法,也遵循 Leo 的方法,
func formatMmSs(counter: Double) -> String {
let minutes = Int(counter) / 60 % 60
let seconds = Int(counter) % 60
let milliseconds = Int(counter*1000) % 1000
return String(format: "%02d:%02d:%03d", minutes, seconds, milliseconds)
}