在開發(fā)中經(jīng)常會(huì)用到倒計(jì)時(shí)的按鈕,常用在發(fā)送短信驗(yàn)證碼中。最差的寫法是把代碼寫在ViewController中。這樣的話如果項(xiàng)目中存在多個(gè)倒計(jì)時(shí)按鈕,代碼就十分臃腫了。所以封裝一個(gè)倒計(jì)時(shí)按鈕就迫在眉睫了。傳送門HLCountDownButton,歡迎幫忙點(diǎn)點(diǎn)小星星????
- ? 支持過(guò)
xib、storyboard自定義樣式 - ? 支持切換前后臺(tái)計(jì)時(shí)準(zhǔn)確
- ? 多種樣式切換
- ? 支持
CocoaPods導(dǎo)入pod "HLCountDownButton" - ? 支持全局設(shè)置樣式
演示圖

QQ20220610-112617.png

gif
可選自定義屬性
/// 樣式,默認(rèn)HLCountDownButtonTypeNormal
@property (nonatomic, assign) IBInspectable NSInteger hlType UI_APPEARANCE_SELECTOR;
/// 是否可用,默認(rèn)YES
@property (nonatomic, assign) IBInspectable BOOL hlEnabled UI_APPEARANCE_SELECTOR;
/// 正常標(biāo)題,默認(rèn)“獲取驗(yàn)證碼”
@property (nonatomic, copy) IBInspectable NSString *normalTitle UI_APPEARANCE_SELECTOR;
/// 重新獲取標(biāo)題,默認(rèn)“獲取驗(yàn)證碼”
@property (nonatomic, copy) IBInspectable NSString *againTitle UI_APPEARANCE_SELECTOR;
/// 發(fā)送中標(biāo)題格式,默認(rèn)“%ds 后獲取”
@property (nonatomic, copy) IBInspectable NSString *sendingTitleFormat UI_APPEARANCE_SELECTOR;
/// 高亮顏色,默認(rèn)0x4181FE
@property (nonatomic, strong) IBInspectable UIColor *highlightedColor UI_APPEARANCE_SELECTOR;
/// 非高亮顏色,默認(rèn)0xd2d2d2
@property (nonatomic, strong) IBInspectable UIColor *disabledColor UI_APPEARANCE_SELECTOR;
/// 圓角半徑,默認(rèn)4
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius UI_APPEARANCE_SELECTOR;
/// 邊框?qū)挾龋J(rèn)0.5
@property (nonatomic, assign) IBInspectable CGFloat borderWidth UI_APPEARANCE_SELECTOR;
/// 倒計(jì)時(shí)長(zhǎng),默認(rèn)60s
@property (nonatomic, assign) IBInspectable NSInteger countDownSize UI_APPEARANCE_SELECTOR;
3種樣式選擇
typedef NS_ENUM(NSInteger, HLCountDownButtonType) {
HLCountDownButtonTypeNormal, ///< 無(wú)背景,無(wú)邊框
HLCountDownButtonTypeOnlyLine, ///< 無(wú)背景,有邊框
HLCountDownButtonTypeOnlyBackground, ///< 有背景,無(wú)邊框
};
回調(diào)說(shuō)明
__weak typeof(self) weakSelf = self;
self.countDownButton.startBlock = ^(){
// TODO 點(diǎn)擊了倒計(jì)時(shí)按鈕,這里可進(jìn)行發(fā)送驗(yàn)證碼操作
// [weakSelf sendCodeMsg];
};
注意:點(diǎn)擊了會(huì)有
startBlock回調(diào),就不要為HLCountDownButton添加點(diǎn)擊Action了
全局樣式設(shè)置
如果您項(xiàng)目中多個(gè)地方使用到該組件,您可以全局設(shè)置樣式,例在AppDelegate添加
[HLCountDownButton appearance].highlightedColor = [UIColor redColor];
注意:
代碼>appearance>interface builder,所以appearance設(shè)置的會(huì)覆蓋在xib或storyboard中設(shè)置的屬性,當(dāng)然代碼會(huì)覆蓋appearance設(shè)置
如果你覺(jué)得封裝的還行,幫忙小星星????支持一下。傳送門HLCountDownButton