iOS验证码倒计时 发表于 2015-11-18 | 分类于 ios iOS验证码倒计时的实现 思路设置全局的Timer对象,全局的计数,假设操作的button为valiButton,点击后定时执行方法,60秒后重置,并取消timer 具体代码123456789101112131415161718192021222324@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; }}