最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
iOS倒计时的实现方法
时间:2022-06-26 06:02:29 编辑:袖梨 来源:一聚教程网
效果
用法
1.导入Timer.h/.m文件
2.所需界面导入头文件 #import “Timer.h”,其他设置参考源码
源码
github:
细节
| |
代码如下 | 复制代码 |
#import "ViewController.h"
#import "Timer.h"
@interface ViewController ()
@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
|
相关文章
- 魔兽世界莫高雷怎么去雷霆崖-莫高雷前往雷霆崖方法 09-18
- 桃源深处有人家晴檐莳药建筑有哪些功能-晴檐莳药甲等建筑可解锁功能介绍 09-18
- 桃源深处有人家颐养四时活动商店有哪些道具-颐养四时活动商店可兑换道具一览 09-18
- 对决剑之川心法获取攻略 09-18
- 地下城堡4酒馆声望等级提升攻略 09-18
- 绝区零机巧心种音擎强度介绍 09-18