不說廢話,上圖說話:
目標
- 退出頁面、App進入後臺依然有效
- 同一計時項目不重複建立
- 驗證碼倒計時、秒殺、活動計時...通用
- 一句或兩句代碼插入即用
看代碼,就是這麼簡單
/// 建立計時,3種方式,相同ID的計時不會重複建立
func countDown(_ tag:Int){
switch tag {
case 0:// 代理接收
CD_CountDown.make(.delegate(self, "123", 120, 0.1))
case 1://廣播
CD_CountDown.make(.notification("456", 120, 0.1))
case 2://閉包回調接收
CD_CountDown.make(.callBack("789", 60, 0.1, { [weak self](model) in
self?.lab_3.cd.text("\(model.day)天\(model.hour):\(model.minute):\(model.second):\(model.millisecond/100)")
}))
default:
break
}
}
複製代碼
/// 接收
extension ViewController:CD_CountDownProtocol {
func cd_countDown(withModel model: CD_CountDown.Model, id: String) {
self.lab_1.cd.text("\(model.day)天\(model.hour):\(model.minute):\(model.second):\(model.millisecond/100)")
}
}
NotificationCenter.default.rx
.notification(Notification.Name(rawValue: "456"), object: nil)
.asObservable()
.subscribe(onNext: { [weak self](n) in
if let model = n.userInfo?["456"] as? CD_CountDown.Model {
self?.title = "\(model.day)天\(model.hour):\(model.minute):\(model.second)"
}
})
.disposed(by: disposeBag)
deinit {
//若是不須要保持 能夠移除
CD_CountDown.remove("456")
}
複製代碼
CD_CountDown.after(10) {[weak self] in
self?....
}
複製代碼
---------- 敬請大佬指正 ----------