今天在CocoaChina上面看到有人在问倒计时怎么做,记得以前在看Iphone31天的时候做过一个,今天翻出来运行不了了,原因是我的IphoneSDK升级到3.1了,以前使用的是2.2.1,在2.2.1里面是可以使用NSCalendarDate的,但是在3.1里面不能够使用,怎么办,只好用NSTimer了,最后还是给实现了。代码也比较简单,开始运行viewDidLoad的时候加载 [NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(timerFireMethod:) userInfo:nilrepeats:YES];//使用timer定时,每秒触发一次
,然后就是写selector了。
 
-(void)timerFireMethod:(NSTimer*)theTimer
{
 //NSDateFormatter *dateformatter =[[[NSDateFormatter alloc]init]autorelease];//定义NSDateFormatter用来显示格式
 //[dateformatter setDateFormat:@"yyyy MM dd hh mmss"];//设定格式
 NSCalendar *cal = [NSCalendarcurrentCalendar];//定义一个NSCalendar对象
 NSDateComponents *shibo = [[NSDateComponentsalloc] init];//初始化目标时间(好像是世博会的日期)
 [shibo setYear:2010];
 [shibo setMonth:5];
 [shibo setDay:1];
 [shibo setHour:8];
 [shibo setMinute:0];
 [shibo setSecond:0];
 
 NSDate *todate = [caldateFromComponents:shibo];//把目标时间装载入date
 [shibo release];
// NSString *ssss = [dateformatterstringFromDate:dd];
// NSLog([NSString stringWithFormat:@"shiboshi:%@",ssss]);
 
 NSDate *today = [NSDate date];//得到当前时间
// NSString *sss = [dateformatterstringFromDate:today];
// NSLog([NSString stringWithFormat:@"xianzaishi:%@",sss]);
 //用来得到具体的时差
 unsigned int unitFlags = NSYearCalendarUnit |NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
 NSDateComponents *d = [cal components:unitFlagsfromDate:today toDate:todate options:0];
 lab.text = [NSStringstringWithFormat:@"%d年%d月%d日%d时%d分%d秒",[d year],[d month], [d day],[d hour], [d minute], [d second]];
}
这样就实现了倒计时的功能。

使用NSTimer实现倒计时-备的更多相关文章

  1. 用nstimer实现倒计时

    用nstimer实现倒计时 // [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFir ...

  2. 倒计时实现两种方法-NSTimer/GCD

    #import "ViewController.h" @interface ViewController () @property (nonatomic ,strong)UIBut ...

  3. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  4. swift - 利用UIDatePicker实现定时器的效果

    效果图如下: 可以通过UIDatePicker调整倒计时的时间,然后点击UIButton开始倒计时,使用NSTimer进行倒计时的时间展示,我是声明了一个label也进行了标记, 然后点击按钮开始倒计 ...

  5. NSTimer+倒计时功能实现

    NSTimer 一.前言,查看官方文档,可以发现NSTimer是Foundation框架下的一个类,它直接继承与NSObject. 二.常用属性 1. @property (copy) NSDate ...

  6. iOS 倒计时NSTimer

    项目中可能会遇到有些倒计时的地方 比方 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就须要展示一个倒计时 详细实现方式是使用了iOS 自带的 NSTimer 上代码 首先新建 ...

  7. NSTimer实现读秒、倒计时等周期性操作

    self.timerSchedule = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(spin ...

  8. iOS 商品倒计时 限时特价 限时优惠 功能的封装

    最近项目中多个页面用到了 商品特价倒计时的功能  为了偷懒 于是自己封装了一个限时抢购 倒计时的view 代码实现如下: 定向价 限时特价 模型代码实现: #pragma mark 商品定向价模型 @ ...

  9. 比较全的JavaScript倒计时脚本[xyytit]

    需要做一个功能,给特定的活动或者商品添加一个倒计时提示,在网上找了好些方法,总结了比较好的一些方法,以备后用: 1. 比较长时间的倒计时(如:距离2014年还有0年, 0月, 30天, 9小时, 41 ...

随机推荐

  1. webpy,希望能多了解一些关于WSGI,PYTHON的WEB开发框架的事,也希望能进一步了解PYTHON

    如果能真正看懂源代码,那就强了. 几年了,不应该总是小搞小打的. [Python]Webpy 源码学习(一) http://diaocow.iteye.com/blog/1922760 学习线路: 那 ...

  2. atof

    So given a string like "2.23" your function should return double 2.23. This might seem eas ...

  3. Delphi判断进程是否存在(使用CreateToolhelp32Snapshot)

      program Project2; uses windows,TLHelp32; function FindProcess(AFileName:string):boolean; var hSnap ...

  4. 分布式文件系统 FastDFS Ceph

    分布式文件系统 FastDFS Cephhttp://www.oschina.net/p/fastdfshttp://www.oschina.net/p/ceph FastDFS 的 Go 客户端 f ...

  5. PowerShell 转码函数 Default->UTF8

    $chs='你好' function converToUft8($str){ $bs=[System.Text.Encoding]::Default.GetBytes($str); $u8Char=[ ...

  6. 通过并行 提高批量审核PDF性能

    上一篇文章提到了 通过 iTextSharp 实现PDF 审核盖章 ,如果当需要一次审核大批量的PDF我们如何来提高程序的性能呢? 下面我们通过并行计算来提升性能. 首先是一个审核PDF的方法 pub ...

  7. Wish | IT桔子

    Wish | IT桔子 Wish www.wish.com   认领 关注 分享

  8. hdu4111 Alice and Bob

    Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 深度分析Linux下双网卡绑定七种模式

    现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多.而一般企业都会使用linux操作系统下自带的网卡绑定模式,当然现在网卡产商也会出一些针对windows操 ...

  10. Android笔记(一):从this关键字发散

    this指的是直接包含它的类的实例. 例如: public class MyClass{ int num; public MyClass(int num){ this.num = num; } } 这 ...