iOS 多线程倒计时


在项目中,我们经常遇到有一个过期的时间,而这个过期的时间点后台又没有给,只给了个开始时间,因此我们需要自己去计算解决这个过期问题了.


//倒计时相关代码

NSInteger orderTime = [TimeHandelTool timeSwitchTimestamp:model.OrderTime andFormatter:@"YYYY-MM-dd hh:mm"];

NSLog(@"%ld",orderTime);

NSInteger nowTime = [TimeHandelTool getNowTimestamp];

NSLog(@"%ld",nowTime);

__block NSInteger timeCha = nowTime - orderTime;

NSInteger overdue = 24 * 60 * 60;//过期时间24小时

__block NSInteger leftTime = overdue - timeCha;

if (!(timeCha < overdue)) {

self.leftTimeLabel.text = @"超过支付时间";

self.payButton.enabled = NO;

}else {

//倒计时

self.payButton.enabled = YES;

//获取全局队列

dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

//创建一个定时器,并将定时器的任务交给全局队列执行(并行,不会造成主线程阻塞)

dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);

// 设置触发的间隔时间

dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);

//1.0 * NSEC_PER_SEC  代表设置定时器触发的时间间隔为1s

//0 * NSEC_PER_SEC    代表时间允许的误差是 0s

//block内部 如果对当前对象的强引用属性修改 应该使用__weak typeof(self)weakSelf 修饰  避免循环调用

__weak typeof(self)weakSelf = self;

//设置定时器的触发事件

dispatch_source_set_event_handler(timer, ^{

leftTime --;

if (leftTime <= 0) {

//关闭定时器

dispatch_source_cancel(timer);

weakSelf.leftTimeLabel.text = @"超过支付时间";

weakSelf.payButton.enabled = NO;

//MRC下需要释放,这里不需要

// dispatch_realse(timer);

dispatch_async(dispatch_get_main_queue(), ^{

[weakSelf.leftTimeLabel setText:@"超过支付时间"];

});

}else {

//主线程修改UI

weakSelf.payButton.enabled = YES;

dispatch_async(dispatch_get_main_queue(), ^{

NSInteger hour = leftTime / 3600;

NSInteger min = (leftTime - hour * 3600)/60;

NSString * title = [NSString stringWithFormat:@"%ld小时%ld分钟",hour,min];

weakSelf.leftTimeLabel.text = title;

});

}

});

dispatch_resume(timer);

}


时间处理的相关代码

对时间相关的进行封装,一个项目里面有可能其他地方也会用到


.h文件代码如下

#import@interface TimeHandelTool : NSObject

//获取当前时间的 时间戳

+(NSInteger)getNowTimestamp;

//将某个时间转化成 时间戳

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format;

//将某个时间戳转化成 时间

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format;

@end


.m文件

#import "TimeHandelTool.h"

@implementation TimeHandelTool

#pragma mark - 获取当前时间的 时间戳

+(NSInteger)getNowTimestamp{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式

//设置时区

NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[formatter setTimeZone:timeZone];

NSDate *datenow = [NSDate date];//现在时间

//时间转时间戳的方法:

NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];

NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值

return timeSp;

}

//将某个时间转化成 时间戳

#pragma mark - 将某个时间转化成 时间戳

+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式

NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[formatter setTimeZone:timeZone];

NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate

//时间转时间戳的方法:

NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];

return timeSp;

}

//将某个时间戳转化成 时间

#pragma mark - 将某个时间戳转化成 时间

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

[formatter setDateStyle:NSDateFormatterMediumStyle];

[formatter setTimeStyle:NSDateFormatterShortStyle];

[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------设置你想要的格式

NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[formatter setTimeZone:timeZone];

NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];

NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];

return confromTimespStr;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前一段时间,公司要求传递各种时间类型,以及各种时间戳的转化,因此,自己抽了一点时间去转化这个时间类型,最后,把大部...
    葫芦村村长阅读 5,206评论 0 0
  • 在iOS开发中,经常会遇到各种各样的时间问题,8小时时差,时间戳,求时间间隔,农历等等。解决办法网上比比皆是,但大...
    真巧了_嘿阅读 7,758评论 0 7
  • 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了。今年,找过工作人可能会更深刻地体会到今年的就...
    独酌丿红颜阅读 6,978评论 18 60
  • 来自网络 序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了。今年,找过工作人可能会更深刻地体会...
    用心在飞阅读 4,253评论 5 4
  • http://www.devstore.cn/essay/essayInfo/6525.html【原文地址】 序言...
    起名好难_fz阅读 3,851评论 1 1