如何在IOS开发中实现倒计时/倒计时代码
来源:爱站网时间:2020-11-05编辑:网友分享
在IOS应用程序开发过程中,经常需要获取验证码,而我们程序员要实现的是让这个倒计时提示用户,本文是爱站技术频道小编为大家介绍的如何在IOS开发中实现倒计时/倒计时代码,一起参考下面的介绍吧!
在IOS应用程序开发过程中,经常需要获取验证码,而我们程序员要实现的是让这个倒计时提示用户,本文是爱站技术频道小编为大家介绍的如何在IOS开发中实现倒计时/倒计时代码,一起参考下面的介绍吧!
示例代码简单易懂:

#import@interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *dayLabel; @property (weak, nonatomic) IBOutlet UILabel *hourLabel; @property (weak, nonatomic) IBOutlet UILabel *minuteLabel; @property (weak, nonatomic) IBOutlet UILabel *secondLabel; @end
#import
"ViewController.h"
@interface
ViewController ()
{
dispatch_source_t
_timer;
}
@end
@implementation
ViewController
/**
*
获取当天的年月日的字符串
*
这里测试用
*
@return 格式为年-月-日
*/
-(NSString
*)getyyyymmdd{
NSDate
*now = [NSDate date];
NSDateFormatter
*formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat
= @"yyyy-MM-dd";
NSString
*dayStr = [formatDay stringFromDate:now];
return
dayStr;
}
-
(void)viewDidLoad
{
[super
viewDidLoad];
NSDateFormatter
*dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter
setDateFormat:@"yyyy-MM-dd"];
NSDate
*endDate = [dateFormatter dateFromString:[self getyyyymmdd]];
NSDate
*endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate] +
24*3600)];
NSDate
*startDate = [NSDate date];
NSTimeInterval
timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];
if
(_timer==nil) {
__block
int
timeout = timeInterval; //倒计时时间
if
(timeout!=0)
{
dispatch_queue_t
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0,
0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL,
0),1.0*NSEC_PER_SEC,
0);
//每秒执行
dispatch_source_set_event_handler(_timer,
^{
if(timeout效果:

上述是爱站技术频道小编给大家介绍的如何在IOS开发中实现倒计时/倒计时代码,同学们快点根据上述的步骤学起来吧。今天的介绍就到这里了,如果大家还想了解更多相关的资讯就继续关注js.aizhan.com吧!
上一篇:IOS开发中隐藏状态栏的解决方法
