iOS封装倒计时按钮HLCountDownButton示例详解
作者:一个写iOS的小垃圾
这篇文章主要为大家介绍了iOS封装倒计时按钮HLCountDownButton示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
引言
在开发中经常会用到倒计时的按钮,常用在发送短信验证码中。最差的写法是把代码写在ViewController
中。这样的话如果项目中存在多个倒计时按钮,代码就十分臃肿了。所以封装一个倒计时按钮就迫在眉睫了。传送门HLCountDownButton,欢迎帮忙点点小星星🌟🌟
- ✅ 支持过
xib
、storyboard
自定义样式 - ✅ 支持切换前后台计时准确
- ✅ 多种样式切换
- ✅ 支持
CocoaPods
导入pod "HLCountDownButton"
- ✅ 支持全局设置样式
演示图
可选自定义属性
/// 样式,默认HLCountDownButtonTypeNormal @property (nonatomic, assign) IBInspectable NSInteger hlType UI_APPEARANCE_SELECTOR; /// 是否可用,默认YES @property (nonatomic, assign) IBInspectable BOOL hlEnabled UI_APPEARANCE_SELECTOR; /// 正常标题,默认“获取验证码” @property (nonatomic, copy) IBInspectable NSString *normalTitle UI_APPEARANCE_SELECTOR; /// 重新获取标题,默认“获取验证码” @property (nonatomic, copy) IBInspectable NSString *againTitle UI_APPEARANCE_SELECTOR; /// 发送中标题格式,默认“%ds 后获取” @property (nonatomic, copy) IBInspectable NSString *sendingTitleFormat UI_APPEARANCE_SELECTOR; /// 高亮颜色,默认0x4181FE @property (nonatomic, strong) IBInspectable UIColor *highlightedColor UI_APPEARANCE_SELECTOR; /// 非高亮颜色,默认0xd2d2d2 @property (nonatomic, strong) IBInspectable UIColor *disabledColor UI_APPEARANCE_SELECTOR; /// 圆角半径,默认4 @property (nonatomic, assign) IBInspectable CGFloat cornerRadius UI_APPEARANCE_SELECTOR; /// 边框宽度,默认0.5 @property (nonatomic, assign) IBInspectable CGFloat borderWidth UI_APPEARANCE_SELECTOR; /// 倒计时长,默认60s @property (nonatomic, assign) IBInspectable NSInteger countDownSize UI_APPEARANCE_SELECTOR;
3种样式选择
typedef NS_ENUM(NSInteger, HLCountDownButtonType) { HLCountDownButtonTypeNormal, ///< 无背景,无边框 HLCountDownButtonTypeOnlyLine, ///< 无背景,有边框 HLCountDownButtonTypeOnlyBackground, ///< 有背景,无边框 };
回调说明
__weak typeof(self) weakSelf = self; self.countDownButton.startBlock = ^(){ // TODO 点击了倒计时按钮,这里可进行发送验证码操作 // [weakSelf sendCodeMsg]; };
注意:点击了会有startBlock
回调,就不要为HLCountDownButton
添加点击Action
了
全局样式设置
如果您项目中多个地方使用到该组件,您可以全局设置样式,例在AppDelegate
添加
[HLCountDownButton appearance].highlightedColor = [UIColor redColor];
注意:代码
> appearance
> interface builder
,所以appearance设置的会覆盖在xib或storyboard中设置的属性,当然代码
会覆盖appearance
设置
以上就是iOS封装倒计时按钮HLCountDownButton示例详解的详细内容,更多关于iOS封装HLCountDownButton的资料请关注脚本之家其它相关文章!