现在,我正在获取一个倒数计时器,其形式实际上是“ 24:00:00”。问题是计时器在“ 00:00:00”之后保持运行,并开始递减负数。我想要的是计时器停止在0并在24小时重复。我的问题是时间是Data.type Date()的时间。所以我不知道如何用if语句解释如果倒数计时器== 0时textlabel应该更改。我希望您理解这个问题
struct ContentView: View {
// Timer
func countDownString(from referenceDate: Date, until nowDate: Date) -> String
{
let components = calender.dateComponents([.hour, .minute, .second], from: nowDate, to: referenceDate)
return String(format: "%02dh:%02dm:%02ds",
components.hour ?? 00,
components.minute ?? 00,
components.second ?? 00)
}
@State var nowDate: Date = Date()
let referenceDate: Date = Date().addingTimeInterval(20)
let calender = Calendar(identifier: .gregorian)
let formatter = DateFormatter()
var timer: Timer {
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) {_ in
self.nowDate = Date()
}
}
var body: some View {
Text(countDownString(from: referenceDate, until: nowDate)).font(.largeTitle).onAppear(perform: {
_ = self.timer
})
}
}