首页 > 代码库 > aischool 倒计时VIEW封装

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;

    });

}

aischool 倒计时VIEW封装