本文實例為大家分享了iOS倒計時的具體實現代碼,供大家參考,具體內容如下
效果
用法
1.導入Timer.h/.m文件
2.所需界面導入頭文件 #import “Timer.h”,其他設置參考源碼
源碼
github:https://github.com/makingitbest/CountDownTimer
細節
#import "ViewController.h"#import "Timer.h"@interface ViewController ()<TimerDelegate>@property (nonatomic, strong) UIButton *button;@property (nonatomic, strong) Timer *timer;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 倒計時界面 self.timer = [[Timer alloc] initWithFrame:CGRectMake(10, 100, 200, 30)]; self.timer.delegate = self; // 記得遵守代理 self.timer.sceonds = 5; self.timer.layer.borderWidth = 1; self.timer.layer.cornerRadius = 5; self.timer.layer.borderColor = [UIColor orangeColor].CGColor; self.timer.label.font = [UIFont systemFontOfSize:14]; self.timer.label.textColor = [UIColor orangeColor]; [self.view addSubview:self.timer]; self.button = [[UIButton alloc] initWithFrame:CGRectMake(10, 150, 100, 40)]; self.button.layer.borderWidth = 1.0f; self.button.layer.borderColor = [UIColor blackColor].CGColor; [self.button setTitle:@"點擊" forState:UIControlStateNormal]; [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [self.button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [self.view addSubview:self.button]; [self.button addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];}- (void)buttonEvent { // 啟動倒計時的方法,啟動之后設置button點擊失效 [self.timer timerStart]; self.button.enabled = NO; self.button.layer.borderColor = [UIColor grayColor].CGColor;}- (void)timerFinished:(Timer *)timer { // 計時完成之后,button恢復點擊 self.button.enabled = YES; self.button.layer.borderColor = [UIColor blackColor].CGColor;}@end
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答