緣由
對于做這個封裝的目的就是想制造一個簡單易用無風險的驗證碼倒計時控件吧
-
便于在多個項目使用,網絡上也有很多類似的 一般分兩種 :
1.使用NSTimer作為定時器
2.使用GCD作為定時器
1.NSTimer 問題最多的一個,據我了解 Timer會 retain target(有方法 處理這個 HWWeakTimer,具體實現我就不累贅了 請參考,issue還是有的),一直到 倒計時結束,還會在 app 進入后臺以后 出現暫停倒計時等等異常。
2.GCD使用起來真的是有點繁瑣,這里沒有多做研究,應該是可以的 但是相對來說比較繁瑣
于是乎,想到了 ReactiveCocoa ,RAC處理這些問題真的是殺雞焉用宰牛刀的趕腳啊,很輕松,而且 不會 retain 任何類,ViewController dealloc 以后 也會跟隨dealloc 停止倒計時。
Talk is cheap,我們直接上硬菜 地址 ,需要引入ReactiveObjC, 目前已經在兩個上線項目中使用了。
使用示例:
[button countDownChanging:^NSString *(DBCountDownButton *countDownButton, NSUInteger second) {
return [NSString stringWithFormat:@"剩余%lu秒可以重新獲取",(unsigned long)second];
}];
[button countDownFinished:^NSString *(DBCountDownButton *countDownButton, NSUInteger second) {
// 倒計時結束設置button title
return @"點擊重新獲取";
}];
@weakify(button);
[[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
@strongify(button)
// 時間
[button startCountDownWithSecond:10];
}];
Git地址
添加Cocoapods支持~ pod 'DBCountDownButton'
盡情的享用吧