按钮倒计时

第一种方法

self.count = 60;
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        if (_count != 1) {
            _count -= 1;
            [sender setTitle:[NSString stringWithFormat:@"%lds后重新获取" , (long)_count] forState:UIControlStateNormal];
        } else
        {
            [timer invalidate];
            sender.enabled = YES;
            [sender setTitle:@"点击重新获取" forState: UIControlStateNormal];
        }
    }];

第二种方法

self.count = 60;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];

实现timer方法

- (void)timer:(NSTimer *)timer {
    if (_count != 1) {
        _count -= 1;
        [self.codeButton setTitle:[NSString stringWithFormat:@"%lds后重新获取" , (long)_count] forState:UIControlStateNormal];
    } else
    {
        [timer invalidate];
        self.codeButton.enabled = YES;
        [self.codeButton setTitle:@"点击重新获取" forState: UIControlStateNormal];
    }
}

你可能感兴趣的:(按钮倒计时)