@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封装的更多相关文章

  1. ios 照片编辑的view封装

    转:http://www.cnblogs.com/xiaobaizhu/archive/2013/07/03/3170101.html 该控件有旋转,缩放,拖动,剪裁的功能,封装成了一个ImageCr ...

  2. Vue 倒计时组件封装

    项目中需要用到倒计时的功能,封装了一个组件. 代码解读: 1:created周期中获取传入参数时间的剩余秒数: this.initSecondsLeft() 并绑定间隔事件 intervalEvent ...

  3. iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解

    iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解 iOS开发中,我们常常将一块View封装起来,以便于统一管理内部的子控件.如iOS回顾笔记(02)中的"书" ...

  4. iOS边练边学--view的封装

    一.view封装的思路: *如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部的子控件的创建屏蔽起来,不让外界关心 *外界可以传入对应的模型数据给view,view拿到模型数据 ...

  5. ios开发瀑布流框架的封装

    一:瀑布流框架封装的实现思路:此瀑布流框架的封装仿照tableView的底层实现,1:每个cell的frame的设置都是找出每列的最大y值,比较每列的最大y值,将下一个cell放在最大y值最小的那一列 ...

  6. ListView加载性能优化---ViewHolder---分页

    ListView是Android中一个重要的组件,可以使用它加列表数据,用户可以自己定义列表数据,同时ListView的数据加载要借助Adapter,一般情况下要在Adapter类中重写getCoun ...

  7. android标题栏下面弹出提示框(一) TextView实现,带动画效果

    产品经理用的是ios手机,于是android就走上了模仿的道路.做这个东西也走了一些弯路,写一篇博客放在这里,以后自己也可用参考,也方便别人学习. 弯路: 1.刚开始本来用PopupWindow去实现 ...

  8. Authentication of Django

    Django Authentication 用户认证系统 一. Django的认证系统 Django自带一个用户认证系统,用于处理用户账户.群组.许可和基于cookie的用户会话. 1.1 概览 Dj ...

  9. WebApp中的页面生命周期及路由管理

    最近切换到一个新项目,使用的技术栈是Require+Backbone,鉴于对鞋厂webapp框架的了解,发现这个新项目有些缺陷,主要是单纯依赖Backbone造成的,也就是Backbone的好和坏都在 ...

随机推荐

  1. 对LR关联的一些理解

    从接触LR关联的迷茫,到现在略有感悟,小记期间的一些理解. 一开始认识关联是在LR自带学习例子,需要关联session,也知道了自动关联,手动关联以及边录制边关联. 在使用关联的过程中,也学习了web ...

  2. java 猜数字游戏

    作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...

  3. Hdu 4081 最小生成树

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  4. Unity Standard Assets 简介之 Vehicles

    这篇介绍载具资源包Vehicles. 主要包含Aircraft(飞行器)和Car(车辆)两部分,两个文件夹里分别有AircraftGuidelines.txt和CarGuidelines.txt对相关 ...

  5. [转] 《ES6标准入门》读书笔记

    来源:https://segmentfault.com/a/1190000005863641 let和const命令 ES6新增let命令,用于声明变量,是块级作用域. let声明的变量不会像var声 ...

  6. HDU 3306 Another kind of Fibonacci(快速幂矩阵)

    题目链接 构造矩阵 看的题解,剩下的就是模板了,好久没写过了,注意取余. #include <cstring> #include <cstdio> #include <s ...

  7. MVC 缓存实践(一)

    为什么要讲缓存.缓存到底有什么作用? 下面我们来说一个场景我们有一个首页菜单的布局基本是不会经常发生的变化,如果动态生成的 Web 页被频繁请求并且构建时需要耗用大量的系统资源,那么,如何才能改进这种 ...

  8. 李洪强iOS经典面试题144-数据存储

    李洪强iOS经典面试题144-数据存储   数据存储 sqlite中插入特殊字符的方法和接收到处理方法. 除'其他的都是在特殊字符前面加"/",而 ' -> '' .方法:k ...

  9. metasploit模块字典爆破tomcat

    祭出神器MSF 再用auxiliary/scanner/http/tomcat_mgr_login 这个辅助模块爆破下弱口令 这里就用模块自带的字典吧   然后简单配置下.RUN 需要自己定义字典的话 ...

  10. hibernate 异常:Unexpected Exception caught setting

    异常信息:Unexpected Exception caught setting 'outHeight' on 'class com.srpm.core.project.seismicFortific ...