iOS倒计时的实现方法

时间:2022-06-09 03:27:49

本文实例为大家分享了ios倒计时的具体实现代码,供大家参考,具体内容如下

效果

 iOS倒计时的实现方法

用法

1.导入timer.h/.m文件

2.所需界面导入头文件 #import “timer.h”,其他设置参考源码 

源码

 github:https://github.com/makingitbest/countdowntimer

细节

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。