In my code if you press the startButton countdown goes from 60 - 0 . All I want to do is have a countdown from 3 - 0 then countdown from 60 - 0. Think of it mark, set, go then start the countdown timer from 60 seconds to 0.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var timerLabel: UILabel!
var seconds = 60
var timer = Timer()
var isTimerRunning = false
var resumeTapped = false
@IBAction func startButtonTapped(_ sender: UIButton) {
if isTimerRunning == false {
runTimer()
self.startButton.isEnabled = false
}
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
isTimerRunning = true
}
@IBAction func resetButtonTapped(_ sender: UIButton) {
timer.invalidate()
seconds = 60
timerLabel.text = timeString(time: TimeInterval(seconds))
isTimerRunning = false
}
//MARK: - Public Method
func updateTimer(){
if seconds < 1 {
timer.invalidate()
//Send alert to indicate time's up.
} else {
seconds -= 1
timerLabel.text = timeString(time: TimeInterval(seconds))
}
}
func timeString(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}}
Aucun commentaire:
Enregistrer un commentaire