这段时间写公司的一个外包项目,需要用到倒计时:需要显示时分秒,通过在网上搜集资料,找到了2中方法,我把这两种方法结合起来,可以很好的满足这个需求:

1、创建一个类继承自UIlabel,用来展示时分秒的

.h文件

#import <UIKit/UIKit.h>

@interface TimerLab : UILabel

@property (nonatomic,assign)NSInteger second;
@property (nonatomic,assign)NSInteger minute;
@property (nonatomic,assign)NSInteger hour; @end

.m文件

#import "TimerLab.h"
@interface TimerLab ()
@property (nonatomic, strong)NSTimer *timer;
@end @implementation TimerLab - (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.textAlignment = NSTextAlignmentCenter;
self.timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(timeHeadle) userInfo:nil repeats:YES];
}
return self;
} - (void)timeHeadle{ self.second--;
if (self.second==-) {
self.second=;
self.minute--;
if (self.minute==-) {
self.minute=;
self.hour--;
}
} if (self.hour < || self.minute < || self.second < ) {
self.text = @"倒计时结束需要显示的字样";
[self.timer invalidate];
self.timer = nil;
}else
{
self.text = [NSString stringWithFormat:@"%ld:%ld:%ld",(long)self.hour,(long)self.minute,(long)self.second]; }
if (self.second== && self.minute== && self.hour==) {
[self.timer invalidate];
self.timer = nil;
}
} @end

2、我是在cell里面显示的,(这里注意,不能用xib或者SB拖得,那样不行,必须得是手写,具体是因为使用拖得空间,没有走alloc很重,所以在控制器上显示会有问题)

(1)在项目里面具体实现下面这2个方法

#pragma mark 每个cell倒计时的方法/*-------------------------------------------------------*/
- (NSString*)remainingTimeMethodAction:(long long)endTime
{
//得到当前时间
NSDate *nowData = [NSDate date];
NSLog(@"---当前时间:%@",nowData);
NSDate *endData=[NSDate dateWithTimeIntervalSince1970:endTime];
NSLog(@"----结束时间%@",endData);
NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar ];
NSUInteger unitFlags =
NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit;
NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:nowData toDate: endData options:];
NSInteger Hour = [cps hour];
NSInteger Min = [cps minute];
NSInteger Sec = [cps second];
NSInteger Day = [cps day];
NSInteger Mon = [cps month];
NSInteger Year = [cps year];
NSLog( @" From Now to %@, diff: Years: %ld Months: %ld, Days; %ld, Hours: %ld, Mins:%ld, sec:.%ld",
[nowData description], (long)Year, (long)Mon, (long)Day, (long)Hour, (long)Min,(long)Sec );
// NSString *countdown = [NSString stringWithFormat:@"还剩: %zi天 %zi小时 %zi分钟 %zi秒 ", Day,Hour, Min, Sec];
NSString *countdown = [NSString stringWithFormat:@"%zi:%zi:%zi",Hour, Min, Sec];
NSLog(@"0000%@",countdown);
if (Sec< && Min< && Hour<) {
countdown=[NSString stringWithFormat:@"进行中"];
}
return countdown;
} //时间转化时间戳
-(NSString *)checksting:(NSString *)datas
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate *date = [formatter dateFromString:datas];
NSString *timerStrs = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
return timerStrs;
} #pragma mark/*------------------------------------------------------------------------- */

3,cell里面的具体赋值操作

 //1、 获得到期时间串
NSString *timSts = [NSString stringWithFormat:@"%@",model.time2]; //2、 转化成到时时间戳,并传进对比方法里面
NSString *timerStrs = [NSString stringWithFormat:@"%@",[self remainingTimeMethodAction:[[self checksting:timSts] longLongValue]]]; //3、 分割字符串,取出时分秒
NSArray *ary = [timerStrs componentsSeparatedByString:@":"];
NSLog(@"ary is %@",timerStrs); if (ary.count == ) {
cell.YB_dqTimeLab.hour = [[ary objectAtIndex:] integerValue];
cell.YB_dqTimeLab.minute = [[ary objectAtIndex:] integerValue];
cell.YB_dqTimeLab.second = [[ary objectAtIndex:] integerValue];
}else if(ary.count == )
{
cell.YB_dqTimeLab.hour = ;
cell.YB_dqTimeLab.minute = ;
cell.YB_dqTimeLab.second = ;
}

我是用了别人写好的倒计时显示用的自定义的label,然后使用时间比对的方法,算出时间差,然后进行倒计时的操作!如有理解不到的地方

还希望大家指正!

最新文章

  1. Linux系统VNC配置实践总结
  2. 程序员必须知道的几个Git代码托管平台
  3. Spark入门实战系列--6.SparkSQL(下)--Spark实战应用
  4. Linux基础入门(20135207 王国伊)
  5. ccpc-1008-HDU5839Special Tetrahedron-计算几何
  6. Tomcat Can&#39;t load AMD 64-bit .dll on a IA 32
  7. C#计算某一些任务的执行时间(消耗时间)
  8. 用CorelDRAW勾画对象轮廓图的方法
  9. codeforces 468B 2-sat
  10. EqualsBuilder和HashCodeBuilder(重写equal和hashcode)
  11. Android常见开源解决方案
  12. angular在ie8下的一个bug
  13. 谈谈文件增量同步算法:RSYNC和CDC
  14. fiddler2 中文乱码问题
  15. @font-face 使用过程
  16. 利用kibana插件对Elasticsearch进行文档和索引的CRUD操作
  17. Java中String和byte[]间的转换浅析
  18. LeetCode Weekly Contest 32
  19. Practical Node.js (2018版) 14章, async code in Node
  20. golang编程之我见

热门文章

  1. Ubuntu系统使用命令禁用触摸板等输入设备
  2. php简单混淆类加密文件如何解密?
  3. FPGA的EPCS 配置的2种方法 FPGA下载程序的方法(EPCS)
  4. pandas contact 之后,若要用到index列,要记得用reset_index去处理index
  5. SQLyog之MySQL客户端的下载、安装和使用(企业版)(破解)
  6. scrapy添加 请求头
  7. Ubuntu下利用vim搭建python开发环境
  8. layui数据表格自定义每页条数limit
  9. makefile之include
  10. 关于linux下配置python3的virtualenvwrapper