十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
这篇文章将为大家详细讲解有关iOS如何获取短信验证码倒计时,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、坡头网络推广、成都微信小程序、坡头网络营销、坡头企业策划、坡头品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;成都创新互联公司为所有大学生创业者提供坡头建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com方法一:
网上用的很多的一种,不多说,直接上代码.
-(void)startTime{ __block int timeout= 60; //倒计时时间 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(), ^{ [self.getIdentifyCodeBt setTitle:@"获取验证码" forState:UIControlStateNormal]; self.getIdentifyCodeBt.userInteractionEnabled = YES; [self.getIdentifyCodeBt setTitleColor:THEME_RED forState:UIControlStateNormal]; self.getIdentifyCodeBt.backgroundColor = [UIColor whiteColor]; self.getIdentifyCodeBt.layer.borderColor = THEME_RED.CGColor; }); }else{ dispatch_async(dispatch_get_main_queue(), ^{ [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1]; [self.getIdentifyCodeBt setTitle:[NSString stringWithFormat:@"%zd秒后失效",timeout] forState:UIControlStateNormal]; [self.getIdentifyCodeBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; self.getIdentifyCodeBt.backgroundColor = [UIColor lightGrayColor]; self.getIdentifyCodeBt.layer.borderColor = [UIColor clearColor].CGColor; self.getIdentifyCodeBt.clipsToBounds = YES; [UIView commitAnimations]; self.getIdentifyCodeBt.userInteractionEnabled = NO; }); timeout--; } }); dispatch_resume(_timer); }
到时直接调用就可以了。
方法二:利用分类
给UIButton新建一个分类
.h文件如下
#import@interface UIButton (XSCountDown) - (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration; - (void)xs_stopCountDown; @end
.m文件如下
#import "UIButton+XSCountDown.h" #import "ThemeColor.h" static NSTimer *_countTimer; static NSTimeInterval _count; static NSString *_title; @implementation UIButton (XSCountDown) - (void)xs_beginCountDownWithDuration:(NSTimeInterval)duration { _title = self.titleLabel.text; _count = duration; _countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(xs_updateTitle) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:_countTimer forMode:NSRunLoopCommonModes]; self.userInteractionEnabled = NO; [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; self.backgroundColor = [UIColor lightGrayColor]; self.layer.borderColor = [UIColor clearColor].CGColor; self.clipsToBounds = YES; } - (void)xs_stopCountDown { [_countTimer invalidate]; _countTimer = nil; _count = 60.0; [self setTitle:_title forState:UIControlStateNormal]; self.userInteractionEnabled = YES; } - (void)xs_updateTitle { NSString *countString = [NSString stringWithFormat:@"%lis 后失效", (long)_count - 1]; self.userInteractionEnabled = NO; [self setTitle:countString forState:UIControlStateNormal]; if (_count-- <= 1.0) { [self xs_stopCountDown]; [self setTitleColor:THEME_RED forState:UIControlStateNormal]; self.backgroundColor = [UIColor whiteColor]; self.layer.borderColor = THEME_RED.CGColor; } } @end
然后在controller里直接调用分类.h文件里的方法就ok了
[self.verifyBt xs_beginCountDownWithDuration:60.0];
关于“iOS如何获取短信验证码倒计时”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。