@interface IDCountDownButton : UIButton /** 验证码倒计时的时长 */ @property (nonatomic, assign) NSInteger durationOfCountDown; @end
@interface IDCountDownButton () /** 保存倒计时按钮的非倒计时状态的title */ @property (nonatomic, copy) NSString *originalTitle; /** 保存倒计时的时长 */ @property (nonatomic, assign) NSInteger tempDurationOfCountDown; /** 定时器对象 */ @property (nonatomic, strong) NSTimer *countDownTimer; @end
- (void)setTitle:(NSString *)title forState:(UIControlState)state {
[super setTitle:title forState:state];
// 倒计时过程中title的改变不更新originalTitle
if (self.tempDurationOfCountDown == self.durationOfCountDown) {
self.originalTitle = title;
}
}
- (void)setDurationOfCountDown:(NSInteger)durationOfCountDown {
_durationOfCountDown = durationOfCountDown;
self.tempDurationOfCountDown = _durationOfCountDown;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 设置默认的倒计时时长为60秒
self.durationOfCountDown = 60;
// 设置button的默认标题为“获取验证码”
[self setTitle:@"获取验证码" forState:UIControlStateNormal];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
// 设置默认的倒计时时长为60秒
self.durationOfCountDown = 60;
// 设置button的默认标题为“获取验证码”
[self setTitle:@"获取验证码" forState:UIControlStateNormal];
}
return self;
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
// 若正在倒计时,不响应点击事件
if (self.tempDurationOfCountDown != self.durationOfCountDown) {
return NO;
}
// 若未开始倒计时,响应并传递点击事件,开始倒计时
[self startCountDown];
return [super beginTrackingWithTouch:touch withEvent:event];
}
- (void)startCountDown {
// 创建定时器
self.countDownTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(updateIDCountDownButtonTitle) userInfo:nil repeats:YES];
// 将定时器添加到当前的RunLoop中(自动开启定时器)
[[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];
}
- (void)updateIDCountDownButtonTitle {
if (self.tempDurationOfCountDown == 0) {
// 设置IDCountDownButton的title为开始倒计时前的title
[self setTitle:self.originalTitle forState:UIControlStateNormal];
// 恢复IDCountDownButton开始倒计时的能力
self.tempDurationOfCountDown = self.durationOfCountDown;
[self.countDownTimer invalidate];
} else {
// 设置IDCountDownButton的title为当前倒计时剩余的时间
[self setTitle:[NSString stringWithFormat:@"%zd秒", self.tempDurationOfCountDown--] forState:UIControlStateNormal];
}
}
- (void)dealloc {
[self.countDownTimer invalidate];
}
@interface ViewController () /** 验证码倒计时的button */ @property (nonatomic, strong) IDCountDownButton *vertificationCodeIDCountDownButton; @end
- (void)viewDidLoad {
[super viewDidLoad];
// 创建vertificationCodeIDCountDownButton
self.vertificationCodeIDCountDownButton = [[IDCountDownButton alloc] initWithFrame:CGRectMake(160, 204, 120, 44)];
// 添加点击事件
[self.vertificationCodeIDCountDownButton addTarget:self action:@selector(vertificationCodeIDCountDownButtonClick:) forControlEvents:UIControlEventTouchUpInside];
// 设置标题相关属性
[self.vertificationCodeIDCountDownButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.vertificationCodeIDCountDownButton setTitle:@"获取验证码" forState:UIControlStateNormal];
// 设置背景图片
[self.vertificationCodeIDCountDownButton setBackgroundImage:[UIImage imageNamed:@"redButton"] forState:UIControlStateNormal];
// 设置倒计时时长
self.vertificationCodeIDCountDownButton.durationOfCountDown = 10;
// 将vertificationCodeIDCountDownButton添加的控制器的view中
[self.view addSubview:self.vertificationCodeIDCountDownButton];
}
- (void)vertificationCodeIDCountDownButtonClick:(UIButton *)button {
// TODO:调用服务器接口,获取验证码
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有