• aischool 倒计时VIEW封装


     

    @implementation TWPaperTimeCountLabel

    {

        NSInteger miaoshu;

        dispatch_source_t _timer;

    }

    -(id)initWithframe:(CGRect)frame endTime:(NSDate *)endtime delegate:(id<TWPaperTimelabelDelegate>)delegate

    {

        self = [super initWithFrame:frame];

        if (self) {

            self.delegate = delegate;

            [self setcontentWith:endtime];

        }

        return self;

    }

     

    -(id)initWithframe:(CGRect)frame endTimeStr:(NSString *)endtimestr delegate:(id<TWPaperTimelabelDelegate>)delegate

    {

        self = [super initWithFrame:frame];

        if (self) {

            self.delegate = delegate;

            if (endtimestr!=nil && [endtimestr isKindOfClass:[NSString class]]) {

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

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

                NSDate *destDate= [dateFormatter dateFromString:endtimestr];

                [self setcontentWith:destDate];

            }

        }

        return self;

    }

     

    -(void)reloadTime:(NSString *)endtimestr

    {

        if (endtimestr!=nil && [endtimestr isKindOfClass:[NSString class]]) {

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

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

            NSDate *destDate= [dateFormatter dateFromString:endtimestr];

            [self setcontentWith:destDate];

        }

    }

     

    -(void)setcontentWith:(NSDate *)data

    {

        NSDate *now = [NSDate new];

        NSCalendar *cal = [NSCalendar currentCalendar];

        

        unsigned int unitFlags =   NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

        

        NSDateComponents *d = [cal components:unitFlags fromDate:now toDate:data options:0];

        

        NSInteger alltime = [d day]*3600*24+[d hour]*3600+[d minute]*60+[d second];

        if(alltime>0)

        {

            [self GCDtimeresume:alltime];

        }else

        {

            if (self.delegate!=nil && [self.delegate respondsToSelector:@selector(paperCountdownIsOver:)]) {

                [self.delegate paperCountdownIsOver:self];

            }

        }

    }

     

    -(void)GCDtimeresume:(NSInteger)alltimeSecond

    {

        __block NSInteger timeout=alltimeSecond; //倒计时时间

        

        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); //每秒执行

        WEAKSELF;

        dispatch_source_set_event_handler(_timer, ^{

            if(weakSelf == nil)

            {

                dispatch_source_cancel(_timer);

            }

            

            if(timeout<=0){ //倒计时结束,关闭

                dispatch_source_cancel(_timer);

                dispatch_async(dispatch_get_main_queue(), ^{

                    if (weakSelf.delegate!=nil && [weakSelf.delegate respondsToSelector:@selector(paperCountdownIsOver:)]) {

                        [weakSelf.delegate paperCountdownIsOver:weakSelf];

                    }

                });

            }else{

                

                [weakSelf setlabel:timeout];

                

                timeout--;

            }

        });

        dispatch_resume(_timer);

    }

    -(void)stopcount

    {

        if(_timer)

        {

            dispatch_source_cancel(_timer);

        }

    }

    -(void)setlabel:(NSInteger)alltimeSecond

    {

        NSString *content =@"倒计时: ";

        NSInteger seconds = alltimeSecond % 60;

        NSInteger minutes = (alltimeSecond / 60) % 60;

        NSInteger hours = alltimeSecond / 3600;

        

        if (hours>0) {

            content =[content stringByAppendingString:[NSString stringWithFormat:@"%ld小时",(long)hours]];

        }

        if (minutes>0) {

            content =[content stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)minutes]];

        }

        if (seconds>0) {

            content =[content stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)seconds]];

        }

        dispatch_async(dispatch_get_main_queue(), ^{

            self.text = content;

        });

    }

  • 相关阅读:
    搭建一个属于私人博客
    Python正则表达式的匹配规则
    CentOS 使用yum 安装node.js
    一个单词a,如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的兄弟单词。现在有一个字典,用户输入一个单词,从字典找出这个单词有多少个兄弟单词
    Clion报错 CMake Error at CMakeLists.txt:1 (cmake_minimum_required): CMake 3.
    给定一个整数sum,从n个有序的元素的数组中寻找a,b,使得a+b的结果最接近sum,最快的时间复杂度?
    Go语言通过Docker Go语言SDK获取docker stats的信息
    通过GO程序获取docker version的基本信息
    Go语言实现通过Docker SDK获取docker ps 命令信息&SDK 中docker ps源码解析
    Docker监控docker stats命令的使用与返回参数的意思
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5915488.html
Copyright © 2020-2023  润新知