一百行代码封装商城活动倒计时,用Swift4.2,倒计时格式为天,时,分,秒。
采用DispatchTimer定时器,在globalQueue中执行定时器,只在当前控制器开始倒计时,离开立即停止。
将WMCountDown.swift文件拖拽到项目中。 ViewController使用方法如下:
/// 懒加载 倒计时
lazy var countdownTimer: WMCountDown = {
let timer = WMCountDown()
// 此闭包可以在本类任意方法中写
timer.countDown = { [weak self] (d, h, m, s) in
self?.dayLabel.text = "\(d)天:"
self?.hourLabel.text = "\(h)时:"
self?.minuteLabel.text = "\(m)分:"
self?.secondLabel.text = "\(s)秒"
}
return timer
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
countdownTimer.resume() // 恢复倒计时
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
countdownTimer.suspend() // 停止倒计时
}
override func viewDidLoad() {
super.viewDidLoad()
// 开始倒计时
// 可以传递开始时间参数,用于计算倒计时时间差,不传默认从系统当前时间开始计算时间差
// countdownTimer.start(with: "2018-12-17 22:49:00", end: "2018-12-19 22:49:00")
countdownTimer.start(with: nil, end: "2019-12-19 22:49:00")
}
deinit {
/// 可写可不写
countdownTimer.stop()
}
TableViewCell 使用方法:
class CountDownCell:UITableViewCell{
let countDownTimer =WMCountDown()
override func awakeFromNib() {
super.awakeFromNib()
countDownTimer.countDown= { [weak self ] (d, h, m, s) in
let time = d +":"+ h +":"+ m +":"+ s
self?.textLabel?.text= time
print("时间是:\(d):\(h):\(m):\(s)")
}
// 开始倒计时
// 可以传递开始时间参数,用于计算倒计时时间差,不传,默认从系统当前时间开始计算时间差
// countdownTimer.start(with: "2018-12-17 22:49:00", end: "2018-12-19 22:49:00")*
let minute =randomInRange(range: (1..<60))
let second =randomInRange(range: (1..<60))
countDownTimer.start(with:**nil**, end:"2019-12-19 22:\(minute):\(second)")
}
deinit {
countDownTimer.stop()
}
}