iOS-验证码倒计时60秒

时间:2022-03-29 15:48:46

一. 要求

1.点击获取验证码按钮,60秒倒计时,按钮变成不可点击状态,按钮文字变成倒计时的秒数.

2.当倒计时为0的时候,释放掉定时器NSTimer,按钮变成可以点击状态,按钮文字变成"获取验证码".

二.

//  ViewController.m

//  Demo-验证码

//

//  Created by quhaola on 16/4/11.

//  Copyright © 2016年 MC. All rights reserved.

//

#import "ViewController.h"

#import "Masonry.h"

@interface ViewController ()

{

NSTimer       *_timer;     //定时器

NSInteger      _second;    //倒计时的时间

}

@property (nonatomic, strong) UITextField    * textField;

@property (nonatomic, strong) UIButton       * authCodeButton;

@end

@implementation ViewController

#pragma mark - 生命周期

- (void)viewDidLoad

{

[super viewDidLoad];

//创建验证码按钮

[self addAuthCodeButton];

}

#pragma mark - 实现方法

- (void)addAuthCodeButton

{

//输入框

[self.view addSubview:self.textField];

[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view.mas_left).with.offset(10);

make.top.equalTo(self.view.mas_top).with.offset(100);

make.height.mas_equalTo(40);

make.width.mas_equalTo(200);

}];

//获取验证码按钮

[self.view addSubview:self.authCodeButton];

[self.authCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.textField.mas_right).with.offset(20);

make.right.equalTo(self.view.mas_right).with.offset(-20);

make.top.equalTo(self.view.mas_top).with.offset(100);

make.height.mas_equalTo(40);

}];

}

#pragma mark - 点击事件

//获取验证码按钮的点击事件

- (void)authCodeButtonClicked

{

// 按钮点击之后的操作   --->  按钮不可点击,按钮背景颜色改变

self.authCodeButton.enabled = NO;

self.authCodeButton.backgroundColor = [UIColor grayColor];

// 设置倒计时的总时间

_second = 10;

// 创建计时器

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];

// (可以不考虑) 把清空textField数据

self.textField.text = nil;

}

#pragma mark 定时器调用的方法

- (void)changeTime

{

//每一秒调用一次改方法, 每次调用,_second 减一.

_second --;

//修改倒计时标签文字   ->   把按钮文字改成倒计时的时间

[self.authCodeButton setTitle:[NSString stringWithFormat:@"%@ s",@(_second)] forState:UIControlStateNormal];

//如果时间到了 0 秒, 把定时器取消掉

if (_second == -1)

{

//释放定时器

[_timer invalidate];

//把定时器设置成空.不然不起作用.

_timer = nil;

//把修改的验证码按钮调整为初始状态

self.authCodeButton.enabled = YES;

[self.authCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];

self.authCodeButton.backgroundColor = [UIColor orangeColor];

self.textField.text = @"这里是你请求的验证码";

}

}

#pragma mark - 验证码按钮

- (UITextField *)textField

{

if (!_textField)

{

self.textField = [[UITextField alloc] init];

self.textField.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];

}

return _textField;

}

- (UIButton *)authCodeButton

{

if (!_authCodeButton)

{

self.authCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];

self.authCodeButton.backgroundColor = [UIColor orangeColor];

[self.authCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];

[self.authCodeButton addTarget:self action:@selector(authCodeButtonClicked) forControlEvents:UIControlEventTouchUpInside];

}

return _authCodeButton;

}

@end