美文网首页
iOS时间戳对比倒计时

iOS时间戳对比倒计时

作者: 你的小福蝶 | 来源:发表于2019-03-05 14:47 被阅读0次
    需求:

    Cell中倒计时显示,到期刷新接口,超时数据消失。后端提供订单生成时间戳createdTime。

    计算:

    到期时间 = 订单生成时间+后端订单预留时间
    剩余时间 = 到期时间 - 现在时间
    剩余时间的年,月,日,时,分,秒都小于等于0时即为超时失效。

    工具使用:CountDown
    //导入文件,链接文章末尾
    #import "CountDown.h"
    @property (nonatomic , strong) CountDown *countDown;
    
        //轮询初始化
        self.countDown = [[CountDown alloc] init];
        __weak __typeof(self) weakSelf= self;
        [self.countDown countDownWithPER_SECBlock:^{
            [weakSelf updateTimeInVisibleCells];
        }];
    
    //轮询并对Cell进行赋值
    - (void)updateTimeInVisibleCells{
        NSArray  *cells = self.tableView.visibleCells; //取出屏幕可见ceLl
        for (OpenCardWriteCell *cell in cells) {
            OpenCardFailedModel *model = self.dataArray[cell.tag];
            [cell.readBtn setTitle:[self getNowTimeWithString:validateString(model.createdTime)] forState:UIControlStateNormal];
        }
    }
    
    //计算时间差
    - (NSString *)getNowTimeWithString:(NSString *)aTimeString{
        
        NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
        //30分钟后
        NSString *startStr = validateString(aTimeString);
        NSTimeInterval startTime = [startStr doubleValue]*0.001+1800;
        NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:startTime];
        //对比得到差值
        NSTimeInterval timeInterval = [detaildate timeIntervalSinceDate:nowDate];
        
        int days = (int)(timeInterval/(3600*24));
        int hours = (int)((timeInterval-days*24*3600)/3600);
        int minutes = (int)(timeInterval-days*24*3600-hours*3600)/60;
        int seconds = timeInterval-days*24*3600-hours*3600-minutes*60;
        
        //以下按自己需要使用
        NSString *dayStr;NSString *hoursStr;NSString *minutesStr;NSString *secondsStr;
        dayStr = [NSString stringWithFormat:@"%d",days];
        hoursStr = [NSString stringWithFormat:@"%d",hours];
        if(minutes<10)
            minutesStr = [NSString stringWithFormat:@"0%d",minutes];
        else
            minutesStr = [NSString stringWithFormat:@"%d",minutes];
        if(seconds < 10)
            secondsStr = [NSString stringWithFormat:@"0%d", seconds];
        else
            secondsStr = [NSString stringWithFormat:@"%d",seconds];
        if (hours<=0&&minutes<=0&&seconds<=0) {
            [self queryOrderDataMethod];
            return @"已超时";
        }
        
        if (days) {
            return [NSString stringWithFormat:@"剩余时间  %@天%@:%@:%@", dayStr,hoursStr, minutesStr,secondsStr];
        } else if(hours){
            return [NSString stringWithFormat:@"剩余时间  %@:%@:%@", hoursStr,minutesStr,secondsStr];
        }else
            return [NSString stringWithFormat:@"剩余时间  %@:%@", minutesStr,secondsStr];
    }
    
    //销毁
    - (void)viewDidDisappear:(BOOL)animated{
        [super viewDidDisappear:animated];
        [_countDown destoryTimer];
    }
    
    

    感谢文明大佬的技术支持:CountDown

    参考文章:
    https://blog.csdn.net/minjing_lin/article/details/51783163
    https://blog.csdn.net/m372897500/article/details/46443699
    https://blog.csdn.net/bitcser/article/details/52240587

    相关文章

      网友评论

          本文标题:iOS时间戳对比倒计时

          本文链接:https://www.haomeiwen.com/subject/qngoyqtx.html