[Solved] Swift count time untill button is pressed [closed]


You can create a timer

fileprivate var timer: Timer?
var seconds: Int = 0

func runTimer() { 
     timer = Timer.scheduledTimer(timeInterval: 1, target: self,   selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
}

func updateTimer() {
    seconds += 1  
}

Now its just a matter on when you start the timer.
Options : viewDidLoad , viewWillAppear, viewDidAppear

Then on the IBActions of the button pressed , you capture the seconds passed and stop the timer.

timer?.invalidate()
timer = nil

4

solved Swift count time untill button is pressed [closed]