iOS学习笔记--封装倒计时按钮
2017-10-11 10:32
323 查看
倒计时按钮在项目中经常用到,为了使用方便我做了简单的封装。
下面直接上代码。
在需要使用倒计时按钮的地方初始化。
这里我在ViewController 中初始化,记得引入头文件。
demo 地址:https://github.com/xiaobai0134/XBCountDownButton
下面直接上代码。
#import <UIKit/UIKit.h> typedef void(^setBtnAction)(); @interface countdownButton : UIButton //倒计时开始回调 @property(nonatomic,strong)setBtnAction setBtnAction; @end
#import "countdownButton.h" @interface countdownButton() @property(nonatomic,strong)UIButton * btn; @end @implementation countdownButton { NSInteger secondsCountDown; NSTimer * countDownTimer; } -(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { secondsCountDown = 60; [self setBtnUI]; } return self; } -(void)setBtnUI { [self setBackgroundColor:[UIColor orangeColor]]; [self setTitle:@"获取验证码" forState:UIControlStateNormal]; [self.titleLabel setFont:[UIFont systemFontOfSize:14.0]]; [self addTarget:self action:@selector(countDownAction:) forControlEvents:UIControlEventTouchUpInside]; } -(void)countDownAction:(UIButton *)sender { [self setTitle:[NSString stringWithFormat:@"%zd s",secondsCountDown] forState:UIControlStateNormal]; countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; [self setEnabled:NO]; if(self.setBtnAction) { self.setBtnAction(); } } -(void)timeFireMethod { secondsCountDown--; [self setTitle:[NSString stringWithFormat:@"%zd s",secondsCountDown] forState:UIControlStateNormal]; if (secondsCountDown<0) { [countDownTimer invalidate]; [self setEnabled:YES]; [self setTitle:@"获取验证码" forState:UIControlStateNormal]; secondsCountDown = 60; } } @end
在需要使用倒计时按钮的地方初始化。
这里我在ViewController 中初始化,记得引入头文件。
#import "countdownButton.h"
countdownButton * btn = [[countdownButton alloc]initWithFrame:CGRectMake(100, 100, 90, 40)]; [btn setBackgroundColor:[UIColor purpleColor]]; [btn setSetBtnAction:^(){ //这里写入倒计时开始时需要执行的事件。 NSLog(@"倒计时开始"); }]; [self.view addSubview:btn];
demo 地址:https://github.com/xiaobai0134/XBCountDownButton
相关文章推荐
- 黑马程序员--IOS学习笔记(封装、继承、多态)
- iOS开发基础学习笔记——导航控制器2->导航栏(UINavigationBar)/工具栏(toolBar)/导航元素按钮(UINavigationItem)
- iOS中动态密码(倒计时按钮)按钮Block封装,选取器(UIPickerView)的Block封装,日期选取器(UIDatePicker)的Block封装
- iOS学习笔记-089.彩票04——购彩大厅1_导航栏左边按钮与修改插件
- 黑马程序员-ios学习笔记 oc 封装
- iOS学习笔记之五--标题 frame属性和按钮属性
- react native学习笔记26——iOS原生模块的封装与调用
- iOS学习笔记13— UITableView封装之下拉-上提-图片异步加载
- ios学习之个人笔记(按钮操作)
- ios学习笔记之-点击一个按钮弹出拨打电话提示框
- ios学习笔记之UI篇(四):美化按钮使其看起来具有iphone风格
- iOS音频学习笔记一:常见音频封装格式及编码格式
- ios学习笔记之八--按钮控制图片放大缩小
- iOS学习笔记--如何实现登录按钮显示状态的改变
- iOS学习笔记--瀑布流封装
- IOS学习笔记(三)之UIView的标签(UILabel)与按钮(UIButton)
- IOS学习笔记(三)之UIView的标签(UILabel)与按钮(UIButton)
- IOS开发学习笔记029-反选、全选、删除按钮的实现
- iOS学习笔记-封装含静态库的动态库
- iOS学习笔记-094.彩票09——隐藏底部tabar和统一设置返回按钮