iOS验证码倒计时

思路

设置全局的Timer对象,全局的计数,假设操作的button为valiButton,点击后定时执行方法,60秒后重置,并取消timer

具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@IBOutlet weak var valiButton: UIButton!
//验证码倒计时
var timer:NSTimer!;
var totalNum:Int = 60;

//验证点击事件
@IBAction func valiClick(sender: AnyObject) {
//防止反复点击,因为定时器1s后才执行
self.valiButton.enabled = false;
self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("timerAction"), userInfo: nil, repeats: true);
}

//反复执行的方法
func timerAction(){
if(self.totalNum > 0){
self.totalNum -= 1;
self.valiButton.enabled = false;
self.valiButton.setTitle("\(self.totalNum)", forState: UIControlState.Disabled);
}else{
self.valiButton.enabled = true;
timer.invalidate();
totalNum = 60;
}
}