按钮倒计时

2017-03-28  本文已影响0人  贼海鸥

第一种方法

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];
    }
}
上一篇下一篇

猜你喜欢

热点阅读