我有一个倒数计时器到一个特定的日期。它看起来不错,每秒更新一次以显示倒计时。下面是我用来创建计时器代码:创建一个计时器,每秒钟运行一次?
func scheduleTimeLabelsUpdateTimer() {
var components = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date())
components.second! += 1
let nextSecondDate = Calendar.current.date(from: components)!
let timer = Timer(fireAt: nextSecondDate, interval: 1, target: self, selector: #selector(updateTimeLabels), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .commonModes)
}
不过,我想它在第二更新每一秒,,使在同一时间每个第二时钟更新经过。现在,它会在调用此方法时每秒更新一次,这在viewDidLoad()
中。
例如,如果倒计时设置为午夜,我希望它恰好在午夜时点到零。现在它可能会在午夜后略微达到零,这取决于用户打开此屏幕时的时间差。
编辑:这是如何倒计时显示给用户。 updateTimeLabels()
只是根据在该日期之前剩余的时间量设置每个标签的文本。我希望每一个标签都能在每一秒完全更新。这样倒计时将准确地按时“打零”。注意现在如何,秒数达到零,然后状态栏上的系统时钟更新。我希望这些发生在同一时间。
此代码,这是我几个月前发现某处的堆栈溢出,被称为updateTimeLabels()
来计算剩余时间:
public func timeOffset(from date: Date) -> (days: Int, hours: Int, minutes: Int, seconds: Int) {
// Number of seconds between times
var delta = Double(self.seconds(from: date))
// Calculate and subtract whole days
let days = floor(delta/86400)
delta -= days * 86400
// Caluclate and subtract whole hours
let hours = floor(delta/3600).truncatingRemainder(dividingBy: 24)
delta -= hours * 3600
// Calculate and subtract whole minutes
let minutes = floor(delta/60.0).truncatingRemainder(dividingBy: 60)
delta -= minutes * 60
// What's left is seconds
let seconds = delta.truncatingRemainder(dividingBy: 60)
return (Int(days), Int(hours), Int(minutes), Int(seconds))
}
你是什么意思命中零? –
倒数计时器显示特定日期之前的天数,小时数,分钟数和秒数。如果日期设定为今晚午夜,那么我希望所有这些标签在今晚变成午夜时都准确地说出“0”,而不是一秒钟之后的一部分。 – CompC
它现在的行为如何? –