gpt4 book ai didi

swift - 在特定时间运行计时器(快速)

转载 作者:可可西里 更新时间:2023-11-01 02:05:10 25 4
gpt4 key购买 nike

我有一个应用程序,它从用户那里获取时间和时间间隔,并在那个时间开始倒计时。当我尝试在我拥有的时间启动计时器时,它吓坏了并且无法正常工作。

这是运行定时器的函数:

 func runTimer(){

let timer = Timer(fireAt: firstMealTime, interval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .commonModes)
}

这是 updateTimer 函数:

  if seconds < 1 {
timerRepeat! -= 1
updateMealLeftLabel(numberOfMeal: timerRepeat)
resetTimer()
}else{
seconds -= 1
updateTimerLabel()
}

提前致谢!

最佳答案

您可以像这样检查您的计时器定义:

var seconds = YourCountdownValue // (in seconds) for example 100 seconds.

func runTimer(){
firstMealTime = Date().addingTimeInterval(10.0) // This line is for testing. Countdown will be start in 10 seconds.
let timer = Timer(fireAt: firstMealTime, interval: 1, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: true)
RunLoop.main.add(timer, forMode: .commonModes)
}

func updateTimer() {
if seconds < 1 {
timerRepeat! -= 1
updateMealLeftLabel(numberOfMeal: timerRepeat)
print("finished") //for testing
resetTimer()
}else{
seconds -= 1
print(seconds) //for testing
updateTimerLabel()
}
}

关于swift - 在特定时间运行计时器(快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com