美文网首页
倒计时功能实践 属性字符串

倒计时功能实践 属性字符串

作者: Skysama | 来源:发表于2020-11-25 15:03 被阅读0次
F3C993B5-1415-4F0F-B0E3-451AC2589D81.png

1 获取当前时间 和目标时间 计算相差多少
2 使用gcd 获取倒计时
3 属性字符串 设置 时分秒 的字体和背景色

{
    dispatch_source_t  _timer;
}
        NSString *deadlineStr = self.model.auction.endTime;
                
                NSString *nowStr = [NSString getCurrentTimeyyyymmdd];
                NSInteger secondsCountDown = [NSString getDateDifferenceWithNowDateStr:nowStr deadlineStr:deadlineStr];
                [self setdata:secondsCountDown];

- (void)setdata:(NSInteger)secondsCountDown{
    __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(self->_timer);
                    self->_timer = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
//                        weakSelf.timeLabel.text = @"当前活动已结束";
                        weakSelf.headerView.titleLabel.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.headerView.titleLabel.text = strTime;
                            
                            NSString *endstr =  strTime;
                                                    NSMutableAttributedString *mutablestr = [[NSMutableAttributedString alloc]initWithString:endstr];
                                                    NSDictionary *attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName: ZHColor(140, 190, 250, 1),NSForegroundColorAttributeName:[UIColor whiteColor]};
                                                       [mutablestr setAttributes:attris range:NSMakeRange(7,2)];
                                                      [mutablestr setAttributes:attris range:NSMakeRange(12,2)];
                                                      [mutablestr setAttributes:attris range:NSMakeRange(17,2)];
                                                    weakSelf.headerView.titleLabel.attributedText = mutablestr;
                            
                        } else {

                            NSString *endstr =  [NSString stringWithFormat:@"距结束还有: %ld 天 %02ld 时 %02ld 分 ",days,hours, minute];
                            NSMutableAttributedString *mutablestr = [[NSMutableAttributedString alloc]initWithString:endstr];
                            NSDictionary *attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName: ZHColor(140, 190, 250, 1),NSForegroundColorAttributeName:[UIColor whiteColor]};
                               [mutablestr setAttributes:attris range:NSMakeRange(7,2)];
                              [mutablestr setAttributes:attris range:NSMakeRange(12,2)];
                              [mutablestr setAttributes:attris range:NSMakeRange(17,2)];
                            weakSelf.headerView.titleLabel.attributedText = mutablestr;
                        }
                        
                    });
                    timeout--; // 递减 倒计时-1(总时间以秒来计算)
                }
            });
            dispatch_resume(_timer);
        }
    }
  
}


相关文章

  • 倒计时功能实践 属性字符串

    1 获取当前时间 和目标时间 计算相差多少2 使用gcd 获取倒计时3 属性字符串 设置 时分秒 的字体和背景色

  • js字符串常用操作

    String对象属性 1、length属性 length算是字符串中非常常用的一个属性了,它的功能是获取字符串的长...

  • 前端技术前沿8

    实现倒计时(天数、时、分、秒) parseInt() 函数可解析一个字符串,并返回一个整数。 抢购倒计时功能 抢购...

  • 自动提示宏

    自动提示宏:能在输入属性字符串的时候像属性一样让Xcode有提示功能 一.宏代码: 比如监听view的frame的...

  • JavaScript-字符串应用

    字符串应用 课程简介 主要是学习字符串的属性和字符串的常见方法 字符串属性 字符串常见方法 字符串的属性-leng...

  • 四、字符串

    字符串 一、字符串的属性 字符串的属性length: 表示字符串的长度例如 :var str = “howare...

  • NSAttributedString学习笔记

    NSAttributedString 一种字符串,它的部分文本具有相关属性(如视觉样式、超链接或辅助功能数据)。 ...

  • JS字符串

    字符串属性 字符串方法

  • 【03】包装对象

    【03】包装对象 【01】字符串具有属性和方法: 字符串不是对象,为什么会有属性? 因为引用字符串的属性时,JS会...

  • String(字符)篇

    1.字符串的一些问题 2.字符串属性和方法 字符串属性 字符串方法 javascript == 与 ===区别

网友评论

      本文标题:倒计时功能实践 属性字符串

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