-6

I need to implement a smooth transition to another View using a timer.

For example, when I click on the start button and after 15 seconds pass, the automatic transition to another View takes place.

Current Views

ZGski
  • 2,398
  • 1
  • 21
  • 34

1 Answers1

1

In start button action start a timer with an incrementing counter variable. In the timer execution block if the count reaches 15, invalidate the timer and navigate to next view controller

@objc func startBtnAction(_ sender: UIButton) {
    var count = 0
    let target = 15
    Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] timer in
        self?.label.text = String(format: "%02d:%02d", count/60,count%60)
        if count == target {
            timer.invalidate()
            self?.performSegue(withIdentifier: "NextScreen", sender: self)
        } else {
            count += 1
        }
    }
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70