欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS获取短信验证码倒计时的两种实现方法

程序员文章站 2023-12-19 18:50:10
方法一: 网上用的很多的一种,不多说,直接上代码. -(void)starttime{ __block int timeout= 60; //倒计时时间...

方法一:

网上用的很多的一种,不多说,直接上代码.

-(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 <uikit/uikit.h>

@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];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: