一般倒计时的使用场景就两种:
- 发送短信验证码倒计时
- 广告页倒计时
一、发送短信验证码倒计时
这种情况下,正在倒计时的按钮是不可点击的,也就是说是不可交互的。
二、广告页倒计时
广告页一般都会有个跳过,那么这个倒计时按钮是可点击可交互的。
在倒计时结束时,我们还想让其进入首页
于是乎,就有了下面的分类
@implementation UIButton (CountDown)
/**
倒计时
@param timeLine 倒计时时间
@param title 正常时显示文字
@param subTitle 倒计时时显示的文字(不包含秒)
@param countDoneBlock 倒计时结束时的Block
@param isInteraction 是否希望倒计时时可交互
*/
- (void)countDownWithTime:(NSInteger)timeLine withTitle:(NSString *)title andCountDownTitle:(NSString *)subTitle countDoneBlock:(CountDoneBlock)countDoneBlock isInteraction:(BOOL)isInteraction{
__block NSInteger timeout = timeLine; // 倒计时时间
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); // 每秒执行
dispatch_source_set_event_handler(_timer, ^{
if(timeout<=0){ //倒计时结束,关闭
dispatch_source_cancel(_timer);
dispatch_async(dispatch_get_main_queue(), ^{
if (countDoneBlock) {
countDoneBlock(self);
}
//设置界面的按钮显示 根据自己需求设置
self.userInteractionEnabled = YES;
[self setTitle:title forState:UIControlStateNormal];
[self setTitleColor:[AXUtils colorWithString:kColor16_Theme] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
});
}else{
// int minutes = timeout / 60;
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];;
if (seconds < 10) {
strTime = [NSString stringWithFormat:@"%.1d", seconds];
}
dispatch_async(dispatch_get_main_queue(), ^{
//设置界面的按钮显示 根据自己需求设置
// NSLog(@"____%@",strTime);
[self setTitle:[NSString stringWithFormat:@"%@s%@",strTime,subTitle] forState:UIControlStateNormal];
[self setTitleColor:[AXUtils colorWithString:@"c1c1c1"] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12];
self.userInteractionEnabled = isInteraction;
});
timeout--;
}
});
dispatch_resume(_timer);
}
@end
实现效果:
countDown.gif
网友评论