Reputation: 117
I'm trying to perform a segue when my timer reaches zero and currently having issues. I've removed all code as I'm been trying to give a clearer view before the implementation of the segue. Below I'll post the code of just the time counting down. Any help to get it to segue when done would be appreciated. Thank You in advance
class CountdownViewController: UIViewController {
@IBOutlet weak var iconImage: UIImageView!
@IBOutlet weak var countdownLabel: UILabel!
//Countdown
let futureDate: Date = {
let future = DateComponents(
year: 2020,
month: 6,
day: 16,
hour: 09,
minute: 32 ,
second: 45
)
return Calendar.current.date(from: future)!
}()
var countdown: DateComponents {
return Calendar.current.dateComponents([.day, .hour, .minute, .second], from: Date(), to: futureDate)
}
@objc func updateTime() {
let countdown = self.countdown //only compute once per call
let days = countdown.day!
let hours = countdown.hour!
let minutes = countdown.minute!
let seconds = countdown.second!
self.countdownLabel.text = String(format: "%02d %02d %02d %02d", days, hours, minutes, seconds)
}
override func viewDidLoad() {
super.viewDidLoad()
func runCountdown() {
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: false)
}
runCountdown()
(print(countdown))
(print(countdownLabel as Any))
UIView.animate(withDuration: 3.0 , animations: {
self.countdownLabel.alpha = 0
self.countdownLabel.alpha = 50
self.countdownLabel.alpha = 100
})
UIView.animate(withDuration: 5.0, delay: 0 , options: [.repeat , .autoreverse , .curveEaseIn] , animations: {
let angle = CGFloat(Double.pi)
self.iconImage.transform = CGAffineTransform.init(rotationAngle: (angle))
})
}
}
Upvotes: 0
Views: 134
Reputation: 115002
Essentially all you need to do is compare your futureDate
with the current date and perform the segue if futureDate
is in the past.
I have taken the liberty of removing some code to make my answer clearer (The animations in viewDidLoad
won't achieve anything, for example). I also moved the runCountdown
function out of viewDidLoad
(I suspect that was just a bracket balancing mistake?).
class CountdownViewController: UIViewController {
@IBOutlet weak var iconImage: UIImageView!
@IBOutlet weak var countdownLabel: UILabel!
var timer: Timer?
//Countdown
let futureDate: Date = {
let future = DateComponents(
year: 2020,
month: 6,
day: 16,
hour: 09,
minute: 32,
second: 45
)
return Calendar.current.date(from: future)!
}()
override func viewDidLoad() {
super.viewDidLoad()
self.runCountdown()
}
func runCountdown() {
self.timer = Timer.scheduledTimer(timeInterval: 0.5, repeats: true) { [weak self] timer in
guard let self = self else {
return
}
let now = Date()
guard self.futureDate > now else {
self.performSegue(withIdentifier:"nextScreen",sender: self)
self.timer.invalidate()
self.timer = nil
return
}
let countdown = Calendar.current.dateComponents([.day, .hour, .minute, .second], from: now, to: self.futureDate)
guard let days = countdown.day, let hours = countdown.hour, minutes = countdown.minute, seconds = countdown.second else {
return
}
self.countdownLabel.text = String(format: "%02d %02d %02d %02d", days, hours, minutes, seconds)
}
}
}
Upvotes: 1