點擊獲取驗證碼倒計時

點擊獲取驗證碼倒計時,在項目中很多都會用到,尤其是現在應用App當中手機驗證登錄,都會用到。本著封裝一個倒計時,看到一篇文章非常好,就直接學習了,搞到自己簡書中,用于自己平常項目中,封裝留存。如果不同的建議,可以提出,共同探討與學習!如有侵權,告知,刪除。

創建一個繼承于UIButton類的控件。內部進行樣式的編輯,加一個定時器,適時的移除。當倒計時時,按鈕狀態是不能點擊的。讓倒計時每秒-1,并拼接轉換成NSString。例如:剩余%ld秒。定時器,數字有兩種判斷情況,當定時器數字不等于1時,我們讓按鈕禁用,且數字每秒-1,另一種情況(=1,<0),我們按鈕可以點擊,并移除定時器。改換按鈕內容獲取驗證碼。

示例:

[圖片上傳失敗...(image-475975-1513741678409)]

代碼
GGVerifyCodeViewBtn.h
 #import <UIKit/UIKit.h>

@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
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容