本文實(shí)例為大家分享了iOS倒計(jì)時(shí)的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果
用法
1.導(dǎo)入Timer.h/.m文件
2.所需界面導(dǎo)入頭文件 #import “Timer.h”,其他設(shè)置參考源碼
源碼
github:https://github.com/makingitbest/CountDownTimer
細(xì)節(jié)
#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]; // 倒計(jì)時(shí)界面 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:@"點(diǎn)擊" 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 { // 啟動(dòng)倒計(jì)時(shí)的方法,啟動(dòng)之后設(shè)置button點(diǎn)擊失效 [self.timer timerStart]; self.button.enabled = NO; self.button.layer.borderColor = [UIColor grayColor].CGColor;}- (void)timerFinished:(Timer *)timer { // 計(jì)時(shí)完成之后,button恢復(fù)點(diǎn)擊 self.button.enabled = YES; self.button.layer.borderColor = [UIColor blackColor].CGColor;}@end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選