本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。