美文网首页
按钮倒计时

按钮倒计时

作者: 贼海鸥 | 来源:发表于2017-03-28 10:44 被阅读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];
    }
}

相关文章

网友评论

      本文标题:按钮倒计时

      本文链接:https://www.haomeiwen.com/subject/jmfjottx.html