zoukankan      html  css  js  c++  java
  • 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;

        });

    }

  • 相关阅读:
    LockFree的栈实现及与加锁实现的性能对比
    redis源码笔记-redis.conf
    【ASP.NET】应用程序、页面和控件的生命周期
    【ASP.NET】HTTP客户请求的数据格式说明
    【ASP.NET】页面间传值
    【ASP.NET】Page.IsPostBack 属性
    【ASP.NET】互联网HTTP连接等出错代码大全
    【经验分享】抽象类、虚函数、接口、多态 概念与关系的理解
    【架构设计】需求分析
    【经验分享】常用正则表达式收集
  • 原文地址:https://www.cnblogs.com/xiangjune/p/5915488.html
Copyright © 2011-2022 走看看