ios 简单的倒计时验证码数秒过程实现

  1. timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];  
  1. //倒计时方法验证码实现倒计时60秒,60秒后按钮变换开始的样子  
  2. -(void)timerFireMethod:(NSTimer *)theTimer {  
  3.     if (seconds == 1) {  
  4.         [theTimer invalidate];  
  5.         seconds = 60;  
  6.         [validCodeBtn_ setTitle:@"获取验证码" forState: UIControlStateNormal];  
  7.         [validCodeBtn_ setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  8.         [validCodeBtn_ setEnabled:YES];  
  9.     }else{  
  10.         seconds--;  
  11.         NSString *title = [NSString stringWithFormat:MSG_DYNAMIC_CODE_WAIT,seconds];  
  12.         [validCodeBtn_ setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];  
  13.         [validCodeBtn_ setEnabled:NO];  
  14.         [validCodeBtn_ setTitle:title forState:UIControlStateNormal];  
  15.     }  
  16. }  
  17. //如果登陆成功,停止验证码的倒数,  
  18. - (void)releaseTImer {  
  19.     if (timer) {  
  20.         if ([timer respondsToSelector:@selector(isValid)]) {  
  21.             if ([timer isValid]) {  
  22.                 [timer invalidate];  
  23.                 seconds = 60;  
  24.             }  
  25.         }  
  26.     }  
  27. }  
posted @ 2014-11-26 18:40  ZLK0011  阅读(280)  评论(0编辑  收藏  举报