首页 技术 正文
技术 2022年11月13日
0 收藏 471 点赞 4,809 浏览 3706 个字

代码地址如下:
http://www.demodashi.com/demo/11076.html

在做些活动界面或者限时验证码时, 经常会使用一些倒计时突出展现.

现提供两种方案:

一.使用NSTimer定时器来倒计时

二.使用GCD来倒计时(用GCD这个写有一个好处,跳页不会清零, 跳页清零会出现倒计时错误的)

压缩文件截图项目界面:

项目截图:

一. 使用NSTimer定时器来倒计时

主要步骤:

Step1. 计算截止时间与当前时间差

Step2. 先递减时间差 倒计时-1(总时间以秒来计算)

Step3. 给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示.

获取当天的字符串, 格式为年-月-日 时分秒:

/**
* 获取当天的字符串
*
* @return 格式为年-月-日 时分秒
*/
- (NSString *)getCurrentTimeyyyymmdd { NSDate *now = [NSDate date];
NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
formatDay.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString *dayStr = [formatDay stringFromDate:now]; return dayStr;
}

获取时间差值 截止时间-当前时间:

/**
* 获取时间差值 截止时间-当前时间
* nowDateStr : 当前时间
* deadlineStr : 截止时间
* @return 时间戳差值
*/
- (NSInteger)getDateDifferenceWithNowDateStr:(NSString*)nowDateStr deadlineStr:(NSString*)deadlineStr { NSInteger timeDifference = 0; NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yy-MM-dd HH:mm:ss"];
NSDate *nowDate = [formatter dateFromString:nowDateStr];
NSDate *deadline = [formatter dateFromString:deadlineStr];
NSTimeInterval oldTime = [nowDate timeIntervalSince1970];
NSTimeInterval newTime = [deadline timeIntervalSince1970];
timeDifference = newTime - oldTime; return timeDifference;
}

Step1. 计算时间差值:

NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];

Step2. 递减时间差 倒计时-1(总时间以秒来计算):

secondsCountDown--;

Step3.活动倒计时:

// 启动倒计时后会每秒钟调用一次方法
_activeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(activeCountDownAction) userInfo:nil repeats:YES];
[_activeTimer fire];

给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示:

    // 重新计算 时/分/秒
NSString *str_hour = [NSString stringWithFormat:@"%02ld", secondsCountDown / 3600];
NSString *str_minute = [NSString stringWithFormat:@"%02ld", (secondsCountDown % 3600) / 60];
NSString *str_second = [NSString stringWithFormat:@"%02ld", secondsCountDown % 60];
NSString *format_time = [NSString stringWithFormat:@"%@ : %@ : %@", str_hour, str_minute, str_second];
// 修改倒计时标签及显示内容
self.timeLabel.text = [NSString stringWithFormat:@"使用NSTimer来实现 活动倒计时: %@", format_time]; // 当倒计时结束时做需要的操作: 比如活动到期不能提交
if(secondsCountDown <= 0) { self.timeLabel.text = @"当前活动已结束"; [_activeTimer invalidate];
_activeTimer = nil; return;
}

NSTimer-活动倒计时测试效果如下:

二. 使用GCD来倒计时

主要步骤:

Step1. 计算截止时间与当前时间差

Step2. 用GCD倒计时 给时分秒字符串通过递减过后的秒数,重新计算数值,并输出显示, 递减时间差 倒计时-1

Step1. 计算截止时间与当前时间差:

    // 倒计时的时间 测试数据
NSString *deadlineStr = @"2017-08-19 12:00:00";
// 当前时间的时间戳
NSString *nowStr = [self getCurrentTimeyyyymmdd];
// 计算时间差值
NSInteger secondsCountDown = [self getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];

Step2.使用GCD来实现倒计时

用GCD这个写有一个好处,跳页不会清零 跳页清零会出现倒计时错误的

活动结束等逻辑及界面处理可以按照自己需求来~

    __weak __typeof(self) weakSelf = self;    if (_timer == nil) {
__block NSInteger timeout = secondsCountDown; // 倒计时时间 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 <= 0){ // 当倒计时结束时做需要的操作: 关闭 活动到期不能提交
dispatch_source_cancel(_timer);
_timer = nil;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.timeLabel.text = @"当前活动已结束";
});
} else { // 倒计时重新计算 时/分/秒
NSInteger days = (int)(timeout/(3600*24));
NSInteger hours = (int)((timeout-days*24*3600)/3600);
NSInteger minute = (int)(timeout-days*24*3600-hours*3600)/60;
NSInteger second = timeout - days*24*3600 - hours*3600 - minute*60;
NSString *strTime = [NSString stringWithFormat:@"活动倒计时 %02ld : %02ld : %02ld", hours, minute, second];
dispatch_async(dispatch_get_main_queue(), ^{
if (days == 0) {
weakSelf.timeLabel.text = strTime;
} else {
weakSelf.timeLabel.text = [NSString stringWithFormat:@"使用GCD来实现活动倒计时 %ld天 %02ld : %02ld : %02ld", days, hours, minute, second];
} });
timeout--; // 递减 倒计时-1(总时间以秒来计算)
}
});
dispatch_resume(_timer);
}
}

GCD-活动倒计时测试效果如下:

iOS活动倒计时的两种实现方式

代码地址如下:
http://www.demodashi.com/demo/11076.html

注:本文著作权归作者,由demo大师代发,拒绝转载,转载需要作者授权

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,955
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774