我正在制作一个倒数计时器,让用户可以选择未来的出行日期&时间,点击开始按钮开始倒计时,然后点击重置按钮使计时器无效,并允许用户选择不同的出发日期和时间,然后重新开始。
除了重置按钮外,一切都运行良好。计时器暂时无效,但随后继续倒计时,日,分,小时,秒标签继续更新。
我认为使计时器无效会冻结值,或者我可以将所有日期,分钟,小时,秒标签设置为“”,然后启动按钮将开始新的倒计时。没有这样的运气。在选择新的出发日期/时间后,即使未按下开始按钮,也会开始倒计时。
我正在寻找一种方法来停止printTime和runTimer函数。谢谢您的帮助。
这是代码:
import UIKit
class ViewController: UIViewController {
var resetBtnStatus: Bool = false
var timer = Timer()
let userCalendar = Calendar.current
let requestedComponent: Set<Calendar.Component> = [.day,.hour,.minute,.second]
var departureDateTime: Date?
@IBOutlet weak var departureDateTimePicker: UIDatePicker!
@IBAction func departureDateTimePicker(_ sender: Any) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy H:mm:ss"
departureDateTimePicker.addTarget(self, action: #selector(handler), for: UIControlEvents.valueChanged)
}
@IBOutlet weak var daysLabel: UILabel!
@IBOutlet weak var hoursLabel: UILabel!
@IBOutlet weak var minutesLabel: UILabel!
@IBOutlet weak var secondsLabel: UILabel!
func handler(sender: UIDatePicker) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy H:mm:ss"
let departureDateTimeString = dateFormatter.string(from: departureDateTimePicker.date)
departureDateTime = dateFormatter.date(from: departureDateTimeString)
}
@IBAction func resetBtnPressed(_ sender: Any) {
if resetBtn.isSelected == true {
resetBtnStatus = true
}
departureDateTimePicker.isHidden = false
startBtn.isHidden = false
// let dateFormatter = DateFormatter()
// dateFormatter.dateFormat = "MM/dd/yy H:mm:ss"
// let startTime = Date()
// let endTime = startTime
// let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime)
// daysLabel.text = "\(timeDifference.day!) Days"
// hoursLabel.text = "\(timeDifference.hour!) Hours"
// minutesLabel.text = "\(timeDifference.minute!) Minutes"
// secondsLabel.text = "\(timeDifference.second!) Seconds"
}
// @IBAction func resetBtnPressed(_ sender: Any) {
// if resetBtn.isSelected == true {
// resetBtnStatus = true
// }
// departureDateTimePicker.isHidden = false
// startBtn.isHidden = false
// // let dateFormatter = DateFormatter()
// // dateFormatter.dateFormat = "MM/dd/yy H:mm:ss"
// // let startTime = Date()
// // let endTime = startTime
// // let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime)
// // daysLabel.text = "\(timeDifference.day!) Days"
// // hoursLabel.text = "\(timeDifference.hour!) Hours"
// // minutesLabel.text = "\(timeDifference.minute!) Minutes"
// // secondsLabel.text = "\(timeDifference.second!) Seconds"
// }
func printTime() {
if resetBtnStatus == false {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM/dd/yy H:mm:ss"
let startTime = Date()
let endTime = departureDateTime
let timeDifference = userCalendar.dateComponents(requestedComponent, from: startTime, to: endTime!)
let startTimeDouble: Double = startTime.timeIntervalSinceReferenceDate
var endTimeDouble: Double?
endTimeDouble = (endTime?.timeIntervalSinceReferenceDate)
daysLabel.text = "\(timeDifference.day!) Days"
hoursLabel.text = "\(timeDifference.hour!) Hours"
minutesLabel.text = "\(timeDifference.minute!) Minutes"
secondsLabel.text = "\(timeDifference.second!) Seconds"
if endTimeDouble! > startTimeDouble {
daysLabel.text = "\(timeDifference.day!) Days"
hoursLabel.text = "\(timeDifference.hour!) Hours"
minutesLabel.text = "\(timeDifference.minute!) Minutes"
secondsLabel.text = "\(timeDifference.second!) Seconds"
}
else {
timer.invalidate()
daysLabel.text = ""
hoursLabel.text = ""
minutesLabel.text = ""
secondsLabel.text = ""
}
} else {
timer.invalidate()
daysLabel.text = ""
hoursLabel.text = ""
minutesLabel.text = ""
secondsLabel.text = ""
}
}
func runTimer() {
if resetBtnStatus == false {
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(printTime), userInfo: nil, repeats: true)
timer.fire()
}
if resetBtnStatus == true {
timer.invalidate()
}
}
@IBOutlet weak var startBtn: UIButton!
@IBAction func startBtnPressed(_ sender: Any) {
handler(sender: departureDateTimePicker)
printTime()
runTimer()
departureDateTimePicker.isHidden = true
startBtn.isHidden = true
}
@IBOutlet weak var resetBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}