您的位置:首页 > 移动开发 > IOS开发

按钮倒计时

2016-04-25 10:08 381 查看
首先声明啊,我是一个 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,就好了.

当然了,其他的比如在倒计时过程中,按钮的背景颜色等等,我就不细说了.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS-按钮-倒计时