最近項目中用到了不少定時器匆篓,不管是訂單確認、訂單支付以及驗證碼等都會用到倒計時胖秒,所以就重新的封裝了一個比較好用的缎患,方便使用。天生愚鈍的我可能寫的沒有那么完美阎肝,希望各位大神看后能給出指導的建議挤渔,包括有什么新的需求可以添加的,我好加以修改风题。
說多了都是廢話判导,直接上圖,你能用到并且能夠看上才是真理沛硅。
Timer.gif
下面我來簡單的說一下如何使用這個定時器:
1.0 創(chuàng)建
@objc enum TYCountDownLabelType: Int {
case defaultType // 倒計時
case incrementTimeType // 遞增時間
}
// 方式一
let timerL = TYTimerLabel(label: timerLabel) // 默認倒計時
// 方式二
let timerL = TYTimerLabel(label: timerLabel, type: .defaultType) // .defaultType為倒計時
// 方式三
let timerL = TYTimerLabel(frame: .zero, label: timerLabel, type: .incrementTimeType) // .incrementTimeType遞增時間
2.0 屬性
// 是否開始倒計時
var counting: Bool = false
// 倒計時結束是否重置時間
var isResetTime: Bool = false
// 時間格式類型
fileprivate var _timeFormat: String?
var timeFormat: String? {
get {
if _timeFormat == nil || (_timeFormat?.count ?? 0) == 0 {
_timeFormat = defaultTimeFormatter
}
return _timeFormat
} set {
_timeFormat = newValue
self.dateFormatter?.dateFormat = _timeFormat
self.renewalLabel()
}
}
// dateFormatter
fileprivate var _dateFormatter: DateFormatter?
fileprivate var dateFormatter: DateFormatter? {
get {
if _dateFormatter == nil {
_dateFormatter = DateFormatter.init()
/**
**不同地區(qū)有不同的日期格式眼刃。使用這個方法的目的:得到指定地區(qū)指定日期字段的一個合適的格式
**en_US(United States) : MM/dd/YYYY, HH:mm:ss; en_GB(United Kingdom): dd/MM/YYYY, HH:mm:ss; zh_CN(中國): YYYY/MM/dd HH:mm:ss
**/
_dateFormatter?.locale = Locale(identifier: "en_GB")
/**
**時區(qū): 任何時區(qū)都以GMT為準
** iOS中的時間類NSDate所獲取到的時間, 都是相對于GMT的
**/
_dateFormatter?.timeZone = TimeZone(identifier: "GMT")
_dateFormatter?.dateFormat = self.timeFormat
}
return _dateFormatter
} set {
_dateFormatter = newValue
}
}
3.0 方法
// 開啟定時器
timerL.start()
// 暫停定時器
timerL.pause()
// 重置定時器
timerL.reset()
// 通過傳入時間間隔來設定倒計時的具體時間
timerL.countDownTimeWithTimeInterval(timeInterval: 60) // 設計倒計時一分鐘
// 通過傳入的Date來設定倒計時的具體時間
timerL.countDownTimeWithDate(date: dateTime ?? Date())
// 通過傳入的時間間隔設置遞增時間的初始時間,默認是0:0:0
timerL.incrementTimeWithTimeInterval(interval: 60) // 從一分鐘開始
// 獲取已經過去的時間
let consumeTime = timerL.etConsumeTime()
// 獲取剩余的倒計時時間
let countDownResidueTime = timerL.residueTime()
// 獲取倒計時的總時間
let countDownTime = timerL.getCountDownTime()
3.0 代理
/// 自定義代理
@objc protocol TYCountDownLabelTypeDelegate: NSObjectProtocol {
/**
** 方法: 根據參數的判斷, 更改label的樣式等(根據自己的需求)
** 參數label: 用來展示定時器輸出文字的label
** 參數time: 對于倒計時來說就是當前剩余的時間間隔, 對于遞增時間來說就是當前增加到的時間間隔
**/
@objc optional func changeTimerLabelAttrAtTime(label: UILabel, time: TimeInterval, type: TYCountDownLabelType)
/**
** 方法: 自定義label文字的輸出格式
** 參數label: 用來展示定時器輸出文字的label
** 參數time: 對于倒計時來說就是當前剩余的時間間隔, 對于遞增時間來說就是當前增加到的時間間隔
** 返回值: 自定義的字符串
**/
@objc optional func customTextToDisplayAtTime(label: UILabel, time: TimeInterval) -> String?
/**
** 方法: 自定義label文字的輸出格式
** 參數label: 用來展示定時器輸出文字的label
** 參數time: 倒計時的總時間間隔
**/
@objc optional func countDownTimeOver(label: UILabel, time: TimeInterval)
}
使用起來也是非常的方便稽鞭,你可以根據你們產品的具體的需求來自定義定時器的樣式,根據后臺給你們返回的數據的類型來具體的使用引镊。我寫一個簡單的使用例子:
a. 創(chuàng)建定時器
// MARK: 創(chuàng)建定時器Label
func createTimerLabel() {
let dateFormatter = DateFormatter()
let time = "2018-11-29 12:32:25" // 這里自定義
dateFormatter.dateFormat = "yyy-MM-dd HH:mm:ss"
let dateTime = dateFormatter.date(from: time)
timer = TYTimerLabel.init(label: timerLabel, type: .defaultType)
timer?.countDownTimeWithDate(date: dateTime ?? Date())
// timer?.countDownTimeWithTimeInterval(timeInterval: 60)
timer?.timeFormat = "HH:mm:ss"
timer?.isResetTime = true
timer?.delegate = self
incrementTimer = TYTimerLabel.init(label: incrementTimerLabel, type: .incrementTimeType)
incrementTimer?.incrementTimeWithTimeInterval(interval: 0)
incrementTimer?.timeFormat = "HH:mm:ss SS"
incrementTimer?.delegate = self
}
b. 開始朦蕴、暫停、重置按鈕的實現(xiàn)(不需要按鈕的話直接start()開啟)
// MARK: 開啟定時器
@objc private func startBtnAction(btn: UIButton) {
if btn.tag == 1 {
timer?.start()
} else {
incrementTimer?.start()
}
}
// MARK: 暫停定時器
@objc private func stopBtnAction(btn: UIButton) {
if btn.tag == 2 {
timer?.pause()
} else {
incrementTimer?.pause()
}
}
// MARK: 重置定時器
@objc private func resetBtnAction(btn: UIButton) {
if btn.tag == 3 {
timer?.reset()
} else {
incrementTimer?.reset()
}
}
c.代理方法的實現(xiàn)(產品有特殊的需求)
extension TYTimerViewController: TYCountDownLabelTypeDelegate {
// MARK: 自定義label的樣式
func changeTimerLabelAttrAtTime(label: UILabel, time: TimeInterval, type: TYCountDownLabelType) {
if label == timerLabel {
if time < 55 {
label.textColor = .red
} else {
label.textColor = .white
}
}
}
// MARK: 自定義文字輸出內容
func customTextToDisplayAtTime(label: UILabel, time: TimeInterval) -> String? {
if type == "custom" {
let hours = Int(time / 3600)
let minutes = Int((time - Double(hours * 3600)) / 60)
let seconds = Int(time - Double(hours * 3600) - Double(minutes * 60))
return "\(hours)" + "h" + "\(minutes)" + "min" + "\( seconds)" + "s"
} else {
return nil
}
}
// MARK: 倒計時結束
func countDownTimeOver(label: UILabel, time: TimeInterval) {
label.text = "game over"
}
}
屏幕快照 2018-11-29 下午3.25.18.png
屏幕快照 2018-11-29 下午3.26.24.png