作者:Loving_iOS
轉載請標明出處:http://blog.csdn.net/loving_ios/article/details/50786890
下載倒計時按鈕JKCountDownButton 代碼傳送門
JKCountDownButton,實現iOS倒計時按鈕,常用於註冊等發送驗證碼的時候進行倒計時操作。
下面介紹其用法:
代碼方式建立:
JKCountDownButton *button = [JKCountDownButton buttonWithType:UIButtonTypeCustom];button.frame = CGRectMake(100, 100, 100, 100);[button setTitle:@"擷取驗證碼" forState:UIControlStateNormal];button.backgroundColor = [UIColor blueColor];[self.view addSubview:button]; [button addToucheHandler:^(JKCountDownButton*sender, NSInteger tag) { sender.enabled = NO; [sender startWithSecond:10.0]; [sender didChange:^NSString *(JKCountDownButton *countDownButton,int second) { NSString *title = [NSString stringWithFormat:@"剩餘%d秒",second]; return title; }]; [sender didFinished:^NSString *(JKCountDownButton *countDownButton, int second) { countDownButton.enabled = YES; return @"點擊重新擷取"; }]; }];
用拖控制項方式建立:首先你要button繼承 JKCountDownButton類,type 設定成custom
@property (weak, nonatomic) IBOutlet JKCountDownButton *button; - (IBAction)countDown:(JKCountDownButton*)sender {sender.enabled = NO;//button type要 設定成custom 否則會閃動[sender startWithSecond:10.0]; [sender didChange:^NSString *(JKCountDownButton *countDownButton,int second) { NSString *title = [NSString stringWithFormat:@"剩餘%d秒",second]; return title;}];[sender didFinished:^NSString *(JKCountDownButton *countDownButton, int second) { countDownButton.enabled = YES; return @"點擊重新擷取"; }];}
效果圖: