思路
设置全局的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) { 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; } }
|