定时器结束时执行自动转场?
Perform Auto Segue when Timer is finished?
我正在尝试在我的计时器达到零且当前遇到问题时执行转接。我已经删除了所有代码,因为我一直试图在实施 segue 之前提供更清晰的视图。下面我就post只是时间倒计时的代码。任何帮助让它在完成时继续进行的帮助将不胜感激。提前谢谢你
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))
})
}
}
基本上,您需要做的就是将您的 futureDate
与当前日期进行比较,如果 futureDate
是过去的日期,则执行 segue。
我冒昧地删除了一些代码以使我的答案更清楚(例如,viewDidLoad
中的动画不会实现任何效果)。我还将 runCountdown
函数从 viewDidLoad
中移出(我怀疑这只是括号平衡错误?)。
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)
}
}
}
我正在尝试在我的计时器达到零且当前遇到问题时执行转接。我已经删除了所有代码,因为我一直试图在实施 segue 之前提供更清晰的视图。下面我就post只是时间倒计时的代码。任何帮助让它在完成时继续进行的帮助将不胜感激。提前谢谢你
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))
})
}
}
基本上,您需要做的就是将您的 futureDate
与当前日期进行比较,如果 futureDate
是过去的日期,则执行 segue。
我冒昧地删除了一些代码以使我的答案更清楚(例如,viewDidLoad
中的动画不会实现任何效果)。我还将 runCountdown
函数从 viewDidLoad
中移出(我怀疑这只是括号平衡错误?)。
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)
}
}
}