iOS倒计时的实现方法
程序员文章站
2023-12-20 17:11:34
本文实例为大家分享了ios倒计时的具体实现代码,供大家参考,具体内容如下
效果
用法
1.导入timer.h/.m文件
2.所需界面导入头文件 #i...
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。