设置一个重复的24:00:00倒数计时器,该计时器在00:00:00停止并重置

时间:2020-03-20 15:24:15

标签: timer swiftui

现在,我正在获取一个倒数计时器,其形式实际上是“ 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

                })
}
}

0 个答案:

没有答案