點擊獲取短信驗證碼倒計時(iOS)

今天我們來寫一個簡單的獲取短信驗證碼功能,希望對你有所幫助哦, 先看一下效果:
Demo圖片
  • 寫代碼的時間到了:
//
//  ViewController.m
//
//  Created by 高雅馨 on 16/8/8.
//  Copyright ? 2016年 高雅馨. All rights reserved.
//

#import "ViewController.h"

#define RED_COLOR [UIColor redColor]
#define WIDTH 200
@interface ViewController ()
@property (nonatomic, assign) int x;
@property (nonatomic, assign) int y;
@property (nonatomic, assign) int z;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _x = 0;
    _y = 60;
    _z = 0;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.tag = 3;
    btn.frame = CGRectMake(50, 50, 100, 40);
    [btn addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    UILabel * hongse_Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    hongse_Label.tag = 1;
    hongse_Label.text = @"獲取驗證碼";
    hongse_Label.textColor = [UIColor redColor];
    hongse_Label.font = [UIFont systemFontOfSize:18];
    hongse_Label.backgroundColor = [UIColor whiteColor];
    hongse_Label.layer.borderWidth = 1;
    hongse_Label.layer.borderColor = [UIColor blackColor].CGColor;
    hongse_Label.layer.cornerRadius = 10;
    hongse_Label.layer.masksToBounds = YES;
    hongse_Label.textAlignment = NSTextAlignmentCenter;
    [btn addSubview:hongse_Label];
    
    UILabel * huise_Label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    huise_Label.tag = 2;
    huise_Label.textColor = [UIColor whiteColor];
    huise_Label.backgroundColor = [UIColor grayColor];
    huise_Label.layer.borderColor = [UIColor grayColor].CGColor;
    huise_Label.layer.borderWidth = 1;
    huise_Label.layer.cornerRadius = 10;
    huise_Label.layer.masksToBounds = YES;
    huise_Label.textAlignment = NSTextAlignmentCenter;
    huise_Label.font = [UIFont systemFontOfSize:18];
    huise_Label.alpha = 0.4;
    [btn addSubview:huise_Label];
    
    huise_Label.hidden = YES;
}
- (void)clickBtn {
    UILabel * hongse = (UILabel *)[self.view viewWithTag:1];
    UILabel * huise = (UILabel *)[self.view viewWithTag:2];
    hongse.hidden = YES;
    huise.hidden = NO;
    UIButton * btn = (UIButton *)[self.view viewWithTag:3];
    btn.userInteractionEnabled = NO;
    if (btn.userInteractionEnabled == NO) {
        if (_z == 0) {
            [self timer];
        }else{
            [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer) userInfo:nil repeats:NO];
        }
    }
}
- (void)timer {
    _z = 1;
    UILabel * hongse = (UILabel *)[self.view viewWithTag:1];
    UILabel * huise = (UILabel *)[self.view viewWithTag:2];
    UIButton * btn = (UIButton *)[self.view viewWithTag:3];
    huise.text = [NSString stringWithFormat:@"還剩%d秒",_y];
    _y -= 1;
    
    if (_y == -1) {
        btn.userInteractionEnabled = YES;
        _y = 60;
        hongse.hidden = NO;
        huise.hidden = YES;
        _z = 0;
        [self ok];
    }else{
        [self clickBtn];
    }
}
- (void)ok {
    NSLog(@"循環結束");
}
@end

  • 看到這里是不是覺得特別簡單, 那么就來趕緊實現吧!??
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,283評論 25 708
  • 前言: 點擊獲取驗證碼倒計時,在項目中很多都會用到,尤其是現在應用App當中手機驗證登錄,都會用到。本著封裝一個倒...
    麥穗0615閱讀 4,089評論 1 7
  • 表單中readonly和disabled屬性的區別 設置了readonly的input[text]在提交表單的時候...
    Leamonade閱讀 427評論 0 0
  • 夏語 一場雨, 下滿了離愁, 窗外路人說著不知明的話語。 葉子堆疊著相遇, 泛黃里有暗黑污垢, 不變的路。...
    青廬閱讀 204評論 0 0
  • 我,從小一直就被家長處于放養狀態。我爸忙生意,我媽拼事業。所以從小我就不知道,也不在乎學習。小學,初中,高中。一路...
    Wymanorange閱讀 314評論 0 1