我有一个应用程序,它占用了用户的时间和时间间隔,并在那时开始倒计时。 当我尝试在我拥有的时候启动计时器时,它会变得怪异并且无法正常工作。
这是运行计时器的函数:
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
func:
if seconds < 1 {
timerRepeat! -= 1
updateMealLeftLabel(numberOfMeal: timerRepeat)
resetTimer()
}else{
seconds -= 1
updateTimerLabel()
}
提前感谢!
答案 0 :(得分:1)
您可以像这样检查您的计时器定义:
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()
}
}