按钮倒计时

来源:互联网 发布:如何评价宋思明知乎 编辑:程序博客网 时间:2024/05/22 07:05

首先声明啊,我是一个 iOS 小白,刚开始写.
但是,我发到微博上面的代码都是自己亲手写的,亲测可用,请放心使用

@property (weak, nonatomic) IBOutlet UIButton *btn;@property (nonatomic , assign) NSInteger count;
- (IBAction)btn:(id)sender {    _btn.enabled = NO;    _count = 5;    [_btn setTitle:@"5秒后重新获取" forState: UIControlStateDisabled];    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFire:) userInfo:nil repeats:YES];}- (void)timerFire:(NSTimer *)timer{    if (_count != 1) {        _count -= 1;        [_btn setTitle:[NSString stringWithFormat:@"%ld秒后重新获取" , (long)_count] forState:UIControlStateDisabled];    } else    {        [timer invalidate];        _btn.enabled = YES;        [_btn setTitle:@"点击重新获取" forState: UIControlStateNormal];    }}

这样写出来的按钮倒计时,每次跳动的时候,文字会闪烁.解决办法是把按钮的状态设置为 Custom,就好了.
当然了,其他的比如在倒计时过程中,按钮的背景颜色等等,我就不细说了.

0 0
原创粉丝点击