iOS倒計時的實現(xiàn)方法
更新時間:2017年03月12日 10:56:42 作者:makingitbest
這篇文章主要為大家詳細介紹了iOS倒計時的實現(xiàn)方法,點擊進行倒計時準備,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS倒計時的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果

用法
1.導入Timer.h/.m文件
2.所需界面導入頭文件 #import “Timer.h”,其他設(shè)置參考源碼
源碼
github:https://github.com/makingitbest/CountDownTimer
細節(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];
// 倒計時界面
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 {
// 啟動倒計時的方法,啟動之后設(shè)置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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器
這篇文章主要介紹了iOS開發(fā)中使用UIScrollView實現(xiàn)無限循環(huán)的圖片瀏覽器的方法,感興趣的小伙伴們可以參考一下2016-03-03
iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法
UIDevice最常見的用法就是用來監(jiān)測iOS設(shè)備的電量了,然后再實現(xiàn)電池狀態(tài)通知非常方便,除此之外還有傳感器等信息的獲取,這里我們就來總結(jié)一下iOS App開發(fā)中通過UIDevice類獲取設(shè)備信息的方法:2016-07-07

