封装方法 抢购活动“距开始(结束)x天x小时x秒”并倒计时

项目中要实现如图的效果


1.定时器每秒刷新

- (void)awakeFromNib {

[super awakeFromNib];

self.timeLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0.7];

self.timeLabel.layer.masksToBounds = YES;

self.timeLabel.layer.cornerRadius = 5.0f;

self.timeLabel.textColor = [UIColor darkGrayColor];

self.timeLabel.font = [UIFont systemFontOfSize:14];

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];

}

2.后台获取活动的开始时间和结束时间,调用封装好的方法

-(void)countDownAction

{

//活动开始时间

NSString *startStr = [tools timeWithTimeIntervalString:self.purchaseModel.startTime];

//活动结束时间

NSString *endStr = [tools timeWithTimeIntervalString:self.purchaseModel.endTime];

//显示

NSString *chaString= [tools dateTimeDifferenceWithStartTime:startStr endTime:endStr];

self.timeLabel.text = [NSString stringWithFormat:@"%@",chaString];

}


方法是比较三个时间,当前时间,开始时间和结束时间


+ (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime

{

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

[date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *nowD = [NSDate date];

NSDate *endD = [date dateFromString:endTime];

NSDate *startD = [date dateFromString:startTime];

NSTimeInterval now = [nowD timeIntervalSince1970]*1;

NSTimeInterval end = [endD timeIntervalSince1970]*1;

NSTimeInterval start = [startD timeIntervalSince1970]*1;

//now是现在时间  end是结束的时间

if ((end -now>0)&&( now - start>0)){

//if (end >now>start) {

NSTimeInterval value = end - now;

int second = (int)value %60;//秒

int minute = (int)value /60%60;

int house = (int)value / (24 *3600)%3600;

int day = (int)value / (24 *3600);

NSString *str;

if (day != 0) {

str = [NSString stringWithFormat:@"距结束%d天%d小时%d分%d秒",day,house,minute,second];

}else if (day==0 && house !=0) {

str = [NSString stringWithFormat:@"距结束%d小时%d分%d秒",house,minute,second];

}else if (day==0 && house==0 && minute!=0) {

str = [NSString stringWithFormat:@"距结束%d分%d秒",minute,second];

}else{

str = [NSString stringWithFormat:@"距结束%d秒",second];

}

return str;

}else if (now -start<0)

{

NSTimeInterval value = start-now;

int second = (int)value %60;//秒

int minute = (int)value /60%60;

int house = (int)value / (24 *3600)%3600;

int day = (int)value / (24 *3600);

NSString *str;

if (day != 0) {

str = [NSString stringWithFormat:@"距开始%d天%d小时%d分%d秒",day,house,minute,second];

}else if (day==0 && house !=0) {

str = [NSString stringWithFormat:@"距开始%d小时%d分%d秒",house,minute,second];

}else if (day==0 && house==0 && minute!=0) {

str = [NSString stringWithFormat:@"距开始%d分%d秒",minute,second];

}else{

str = [NSString stringWithFormat:@"距开始%d秒",second];

}

return str;

}else

{

NSString *str = @" 已结束 ";

return str;

}

}


注意:时间label是加载在cell中,所以在定时器写在了awakeFromNib中,原来还纠结写在setmodel方法中 不可行!

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

推荐阅读更多精彩内容

  • 1、改变 UITextField 占位文字 颜色和去掉底部白框 [_userName setValue:[UICo...
    i_MT阅读 1,053评论 0 2
  • -(NSString *)dateTimeDifferenceWithStartTime:(NSString *)...
    猿姑凉阅读 3,538评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,766评论 0 33
  • 20160803写于台风来临之前 哪里的天空在下雨? 堆积的乌云森森如墨, 像沉默的高山, 压...
    紫藤的心事阅读 265评论 0 0
  • 在上海第一次真真切切觉得冷了!对就在今天!下了几天的雨,果然这个时候的雨,一场过后就更冷一些。早上一身睡衣跑出去上...
    众知阅读 165评论 0 1