定时器

来源:互联网 发布:mac 好用的卸载软件 编辑:程序博客网 时间:2024/05/18 06:25

{

  int sum;

  sum=60;

}

@property (nonatomic , strong) NSTimer *timer;//用于重新发送验证码按钮上倒计时


#pragma mark -- 定时器懒加载
- (NSTimer *)timer{
    
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(cutdownTime) userInfo:nil repeats:YES];
        [_timer setFireDate:[NSDate distantFuture]];
    }
    return _timer;
}
//定时器回调方法
- (void)cutdownTime{
    
    _sum -- ;
    NSString *buttonTitle = @"";
    if (_sum >= 0) {
        buttonTitle = [NSString stringWithFormat:@"%ds
再次发送",_sum];
    }else{
        
        buttonTitle = @"
再次发送";
        YZMButton.enabled = YES;
        [YZMButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [self.timer invalidate];//
销毁定时器
        self.timer = nil;
    }
    [YZMButton setTitle:buttonTitle forState:UIControlStateNormal];
    
}


原创粉丝点击