这段时间写公司的一个外包项目,需要用到倒计时:需要显示时分秒,通过在网上搜集资料,找到了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,然后使用时间比对的方法,算出时间差,然后进行倒计时的操作!如有理解不到的地方

还希望大家指正!

iOS开发之--实现倒计时显示时分秒的更多相关文章

  1. ios开发之--把秒转换为天时分秒

    把秒转换成时分秒: - (NSString *)timeFormatted:(int)totalSeconds { ; ) % ; ; return [NSString stringWithForma ...

  2. iOS开发——高级特性&Runtime运行时特性详解

    Runtime运行时特性详解 本文详细整理了 Cocoa 的 Runtime 系统的知识,它使得 Objective-C 如虎添翼,具备了灵活的动态特性,使这门古老的语言焕发生机.主要内容如下: 引言 ...

  3. iOS开发-设置在使用NavigateController时View的顶部位置

      最近我在开发中遇到了一个问题,在使用NavigationController时内部的ViewController的View总是与屏幕顶部对齐,而我们有时候不需要这种效果: 在开发过程中,我们可能会 ...

  4. C# DateTimePicker控件如何精确设置显示时分秒

    项目中需要使用DateTimePicker空间选定当前的时间,但是这个空间默认显示到天,如何能让它显示具体的小时和分钟呢? 我们看到有一个Format属性,Format令我们想到格式化,有这几种格式 ...

  5. iOS开发-自己定义后台显示图片(iOS7-Background Fetch的应用)

    之前在用电池医生的时候, 发现它有这样一个功能:当应用进入后台的时候, 会显示另外一张图片覆盖App Switcher显示的界面. 效果例如以下: 变成----> 而这种一个功能, 对于保护用户 ...

  6. datetimepicker只显示日期,不显示时分秒

    HTML代码<div class="input-group date form_datetime form-date" data-link-field="dtp_i ...

  7. iOS开发 - 在SwiftUI中显示模态视图

    在SwiftUI中显示模态视图 简介 这里教大家如何弹出一个简单的模态视图.分别有两个页面,ContentView和GCPresentedView,以下对应简称为A和B.我们要做的是在A视图中点击按钮 ...

  8. iOS开发——底层OC篇&运行时常用

    运行时常用 什么是Runtime(前面的文章已经说的很清楚了,这里就简单的介绍一下) 我们写的代码在程序运行过程中都会被转化成runtime的C代码执行,例如[target doSomething]; ...

  9. iOS开发cell--滑动手势显示按钮

    // 主要代码 #warning iOS8 - #pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮 - (NSArray *)tableView:(UITableView *)ta ...

随机推荐

  1. nginx配置静态文件过期时间

    1. 编辑虚拟主机配置文件/usr/local/nginx/conf/vhosts/huangzhenping.conf 说明:采用location方式 1 2 3 4 5 6 7 8 9 10  l ...

  2. Linux命令-文件搜索命令:which

    主要用途:查找linu命令,而不是磁盘上的普通文件,并且能看到命令的别名和目录. 区别whereis命令,which在path变量指定的目录中查找命令,并且返回第一个符合的结果.whereis是查找所 ...

  3. List、Set、Map常见集合遍历总结

    Java中的集合有三大类,List.Set.Map,都处于java.util包中,List.Set和Map都是接口,不能被实例化,它们的各自的实现类可以被实例化.List的实现类主要有ArrayLis ...

  4. unity, water cube

    <纪念碑谷>里有一关开始是一个宝箱展开后里面有一个water cube,其中还有小鱼在游.如下截图: 因为我们知道<纪念碑谷>是unity做的,而现在正开始学unity,所以也 ...

  5. blender, knife工具

    点击Knife按钮开始切割,MLB在边上确认分割点,return键完成切割,MRB取消切割(这些在窗口下面的信息提示栏中都有写,如图中如示).

  6. SQL命令查看Mysql数据库大小

    SQL命令查看Mysql数据库大小的方法. 要想知道每个数据库的大小的话,步骤如下:1.进入information_schema 数据库(存放了其他的数据库的信息)use information_sc ...

  7. android:Notification实现状态栏的通知

    在使用手机时,当有未接来电或者新短消息时,手机会给出响应的提示信息,这些提示信息一般会显示到手机屏幕的状态栏上. Android也提供了用于处理这些信息的类,它们是Notification和Notif ...

  8. gdb,程序调试助手

    启动GDB:(其中我的app是编译之后的可执行文件) 退出命令:quit或者简写q 帮助获取: gdb通过完备的在线帮助,使我们使用起来更加方便.所有的帮助信息都是通过help命令获得的,运行help ...

  9. 纯css3实现的竖形二级导航

    之前为大家分享了好多导航菜单.今天给大家带来一款纯css3实现的竖形二级导航.这款导航菜单可以是无限级.一起看下效果图: 在线预览   源码下载 实现的代码. html代码: <div styl ...

  10. [Linux内核]软中断与硬中断

    转自:http://blog.csdn.net/zhangskd/article/details/21992933 本文主要内容:硬中断 / 软中断的原理和实现 内核版本:2.6.37 Author: ...