点击获取验证码倒计时

点击获取验证码倒计时,在项目中很多都会用到,尤其是现在应用App当中手机验证登录,都会用到。本着封装一个倒计时,看到一篇文章非常好,就直接学习了,搞到自己中,用于自己平常项目中,封装留存。如果不同的建议,可以提出,共同探讨与学习!如有侵权,告知,删除。

创建一个继承于UIButton类的控件。内部进行样式的编辑,加一个定时器,适时的移除。当倒计时时,按钮状态是不能点击的。让倒计时每秒-1,并拼接转换成NSString。例如:剩余%ld秒。定时器,数字有两种判断情况,当定时器数字不等于1时,我们让按钮禁用,且数字每秒-1,另一种情况(=1,<0),我们按钮可以点击,并移除定时器。改换按钮内容获取验证码。

示例:

[图片上传失败...(image-475975-1513741678409)]

代码
GGVerifyCodeViewBtn.h
 #import 

@interface GGVerifyCodeViewBtn : UIButton
// 由于有些时间需求不同,特意露出方法,倒计时时间次数
- (void)timeFailBeginFrom:(NSInteger)timeCount;
@end



> GGVerifyCodeViewBtn.m
> 
> #import "GGVerifyCodeViewBtn.h"

  @interface GGVerifyCodeViewBtn ()
  /*
   * 定时器
   */
  @property(strong,nonatomic) NSTimer *timer;


  /*
   * 定时多少秒
   */
  @property(assign,nonatomic) NSInteger count;
  @end


  @implementation GGVerifyCodeViewBtn


  #pragma mark - 初始化控件
  - (instancetype)initWithFrame:(CGRect)frame
  {
      if (self = [super initWithFrame:frame]) {
          // 配置
          [self setup];
    >
      }

      return self;
  }
>
  #pragma mark - 配置


  - (void)setup
  {
  >
      [self setTitle:@"获取验证码" forState:UIControlStateNormal];
      self.titleLabel.font = [UIFont systemFontOfSize:10.f];
      self.backgroundColor = [UIColor yellowColor];
      [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  >
       [self.layer setBorderColor:[UIColor redColor].CGColor];
       [self.layer setBorderWidth:2];
  >
      self.layer.cornerRadius = 3.0f;
      self.layer.masksToBounds = YES;
  >
  }
  #pragma mark - 添加定时器
  - (void)timeFailBeginFrom:(NSInteger)timeCount
  {
      self.count = timeCount;
      self.enabled = NO;
      // 加1个定时器
      self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo: nil repeats:YES];
  >
  >
  }


  #pragma mark - 定时器事件
  - (void)timeDown
  {
      if (self.count != 1){
      >
          self.count -=1;
          self.enabled = NO;
          [self setTitle:[NSString stringWithFormat:@"剩余%ld秒", self.count] forState:UIControlStateNormal];
  >
      } else {
  >
          self.enabled = YES;
          [self setTitle:@"获取验证码" forState:UIControlStateNormal];
          [self.timer invalidate];
      }
  >
  }


##### 用法:

> ViewController.m

  #import "ViewController.h"
  #import "GGVerifyCodeViewBtn.h"


  @interface ViewController ()


  /*
   * 获取验证码按钮
   */
  @property (nonatomic, strong) GGVerifyCodeViewBtn *codeBtn;


  @end


  @implementation ViewController


  - (void)viewDidLoad {
      [super viewDidLoad];
>
      //  获取验证码按钮
      GGVerifyCodeViewBtn *codeBtn = [[GGVerifyCodeViewBtn alloc]init];
      //  设置frame 这里我是按照自己需求来
      codeBtn.frame = CGRectMake(100, 100, 80, 30);
      [self.view addSubview:codeBtn];
       [codeBtn addTarget:self action:@selector(codeBtnVerification) forControlEvents:UIControlEventTouchUpInside];
     self.codeBtn = codeBtn;


  }


  #pragma mark - 获取验证码点击事件
  - (void)codeBtnVerification {
>
    /*
     >
       调用短信验证码接口
       用户输入的验证码数字传给server,判断请求结果作不同的逻辑处理,根据自己家的产品大大需求来即可....
    */
>
   /*
        if (请求成功且匹配成功验证码数字){
        [self.codeBtn timeFailBeginFrom:60];  // 倒计时60s
        } else {
        [self.codeBtn timeFailBeginFrom:1]; // 处理请求成功但是匹配不成功的情况,并不需要执行倒计时功能
        }
    */
>
     [self.codeBtn timeFailBeginFrom:60];
  }


  @end

你可能感兴趣的:(点击获取验证码倒计时)