Я работаю над приложением таймера, которое принимает две переменные как времена для таймера periodOneTime и periodTwoTime. Когда время на одном periodOneTime истекло, я хочу, чтобы мое приложение переключилось на periodTwoTime, и назад и вперед для моего таймера.
Переменная timeLeft под функцией secondTimer содержит эти переменные, чтобы проверить, работает ли таймер, и мне нужен способ проверить, истекло ли время на periodOneTime и periodTwoTime, чтобы я мог переключиться на следующий раз. Я понятия не имею, с чего начать. Спасибо за любую помощь!
@IBAction func startTimer(_ sender: UIButton) {
periodOneTime = Int(periodOne.text!)!
periodTwoTime = Int(periodTwo.text!)!
timeLeft = periodOneTime
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
}
@IBAction func stopTimer(_ sender: UIButton) {
timer?.invalidate()
timeRemaining.text = "0"
}
@objc func onTimerFires()
{
timeLeft -= 1
timeRemaining.text = String(timeLeft)
if timeLeft <= 0 {
timer?.invalidate()
timer = nil
}
//checks if time left is 0, then stops and starts second timer from periodTwo
if timeLeft == 0 {
timer?.invalidate()
secondTimer()
}
}
//Function to switch to second timer (periodTwo)
func secondTimer(){
if timeLeft < 1{
timeLeft = periodTwoTime
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(onTimerFires), userInfo: nil, repeats: true)
}
}