JKCountDownButton
JKCountDownButton 是子类化 UIButton 实现的 iOS 倒计时按钮,常用于注册等发送验证码时执行倒计时操作
JKCountDownButton,子类化 UIButton 实现iOS倒计时按钮,用于注册时发送验证码并进行倒计时
安装
使用 CocoaPods 安装
platform :ios
pod 'JKCountDownButton'
手动安装
将 JKCountDownButton.h 和 JKCountDownButton.m 复制到您的项目中 “JKCountDownButton/” 目录下。
用法
代码
JKCountDownButton *_countDownCode;
_countDownCode = [JKCountDownButton buttonWithType:UIButtonTypeCustom];
_countDownCode.frame = CGRectMake(81, 200, 108, 32);
[_countDownCode setTitle:@"开始" forState:UIControlStateNormal];
_countDownCode.backgroundColor = [UIColor blueColor];
[self.view addSubview:_countDownCode];
[_countDownCode countDownButtonHandler:^(JKCountDownButton*sender, NSInteger tag) {
sender.enabled = NO;
[sender startCountDownWithSecond:10];
[sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
return title;
}];
[sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
countDownButton.enabled = YES;
return @"点击重新获取";
}];
}];
###xib @property (weak, nonatomic) IBOutlet JKCountDownButton *countDownXib;
- (IBAction)countDownXibTouched:(JKCountDownButton*)sender {
sender.enabled = NO;
//button type要 设置成custom 否则会闪动
[sender startCountDownWithSecond:60];
[sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
return title;
}];
[sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
countDownButton.enabled = YES;
return @"点击重新获取";
}];
}
许可证
本代码根据MIT许可证的条款和条件分发。