包涵scheduledTimer秒表小时部分我拿起斯威夫特和Xcode的一个星期前(IM创建一个应用程序在我的假期学习斯威夫特)斯威夫特 - 不工作
我使用scheduledTimer充当秒表创建一个定时器。秒和分钟运行良好,但我无法获得工作时间。我究竟做错了什么?
currentTime = 0
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
self.currentTime += 1
let hoursPortion = String(format: "%02d", CGFloat(self.currentTime) * 60)
let minutesPortion = String(format: "%02d", self.currentTime/60)
let secondsPortion = String(format: "%02d", self.currentTime % 60)
//let tenPortion = String(format: "%d", self.currentTime * 10 % 10)
self.TimerDisplay.text = "\(hoursPortion):\(minutesPortion):\(secondsPortion)"
}
如果当前时间已过秒,则小时数为'currentTime/3600'。我不知道你为什么乘以20.这种方法可能不是最好的;计时器不准确。更好的方法是在秒表开始时创建一个“日期”,并在计时器计时时计算该日期与“现在”之间的差值。 'DateComponents'可以帮助您从日期获取字段。 – Paulw11
是的,这是真的20应该是3600,但我把它设置为20,所以我不必等待一个小时,看看它是否工作。我将hoursPortion设置为乘数,因为我认为1小时= 3600秒。 –
不,你想划分,因为1小时= 3600秒;如果'currentTime' = 3600那么这是1小时,所以'currentTime/3600' – Paulw11