转载于:http://www.cnblogs.com/wujian1360/archive/2011/09/05/2167992.html

创建一个 Timer

  • + scheduledTimerWithTimeInterval: invocation: repeats:
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   invocation:(NSInvocation *)invocation   repeats:(BOOL)yesOrNo;
  • + scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:
  • + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti   target:(id)aTarget   selector:(SEL)aSelector   userInfo:(id)userInfo   repeats:(BOOL)yesOrNo;
  • 创建返回一个新的NSTimer对象和时间表,在当前的默认模式下循环调用一个实例方法。
  • + timerWithTimeInterval: invocation: repeats:
  • + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
  • + timerWithTimeInterval: target:selector: userInfo:repeats:
  • + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
  • – initWithFireDate: interval: target: selector: userInfo: repeats:
  • - (id)initWithFireDate:(NSDate *)date   interval:(NSTimeInterval)ti     target:(id)t    selector:(SEL)s    userInfo:(id)ui    repeats:(BOOL)rep;

scheduledTimerWithTimeInterval:(NSTimeInterval)seconds

预订一个Timer,设置一个时间间隔。

表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1

target:(id)aTarget

表示发送的对象,如self

selector:(SEL)aSelector

方法选择器,在时间间隔内,选择调用一个实例方法

userInfo:(id)userInfo

此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。

repeats:(BOOL)yesOrNo

当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。

invocation:(NSInvocation *)invocation

启动 Timer

  • – fire

停止 Timer

  • – invalidate

Timer设置

  • – isValid
  • – fireDate
  • – setFireDate:
  • – timeInterval
  • – userInfo

NSTimeInterval类:是一个浮点数字,用来定义秒

例子:

iphone为我们提供了一个很强大得时间定时器 NSTimer

他可以完成任何定时功能:

我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理

delegate和事件处理方法@selector();就可以用

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;来初始化一个
时间定时器

下面我写了一个很简单得例子

初始化一个定时器:

-(void)initTimer

{

//时间间隔

NSTimeInterval timeInterval =1.0 ;

//定时器

NSTimer   showTimer = [NSTimer scheduledTimerWithTimeInterval:maxShowTime

target:self

selector:@selector(handleMaxShowTimer:)

userInfo:nil

repeats:NO];

}

//触发事件

-(void)handleMaxShowTimer:(NSTimer *)theTimer

{

NSDateFormatter dateFormator = [[NSDateFormatter alloc] init];

dateFormator.dateFormat = @"yyyy-MM-dd  HH:mm:ss";

NSString *date = [dateformater stringFromDate:[NSDate date]];

if([date isEqualToString:@"2011-11-09 23:59:59"])

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:TITLE_NAME

message:@"现在马上就有新的一天了!"

delegate:self

ancelButtonTitle:nil

otherButtonTitles:CONFIRM_TITLE, nil];

[alert show];

[alert release];

}

[data release];

[dateFormator release];

}

另外附一个例子:方框赛跑

-(void)viewDidLoad

{

[super viewDidLoad];

CGRect workingFrame;

workingFrame.origin.x = 15;

workingFrame.origin.y = 400;

workingFrame.size.width = 40;

workingFrame.size.height = 40;

for(int i = 0; i < 6; i++)

{

UIView *myView = [[UIView alloc] initWithFrame:workingFrame];

[myView setTag:i];//标记方块

[myView setBackgroundColor:[UIColor blueColor]];

workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width + 10;

[self.view addSubview:myView];

}

}

 
  1. // 安装timer(注册timer)
  2. NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 5// 当函数正在调用时,及时间隔时间到了 也会忽略此次调用
  3. target: self
  4. selector: @selector(handleTimer:)
  5. userInfo: nil
  6. repeats: YES]; // 如果是NO 不重复,则timer在触发了回调函数调用完成之后 会自动释放这个timer,以免timer被再一次的调用,如果是YES,则会重复调用函数,调用完函数之后,会将这个timer加到RunLoop中去,等待下一次的调用,知道用户手动释放timer( [timer invalidate];)
  7. [timer invalidate]; // 这个函数将timer从当前的RunLoop中remove掉,必须在timer安装的线程中调用这个函数。
  8. [timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达

NSTimer类的使用的更多相关文章

  1. 【编程技巧】NSTimer类的使用

    创建一个 Timer + scheduledTimerWithTimeInterval: invocation: repeats: + (NSTimer *)scheduledTimerWithTim ...

  2. iOS - CADisplayLink与NSTimer

    一.CADisplayLink简介 CADisplayLink 是一个定时器对象可以让你的应用以与显示器的刷新界面相同的频率进行绘图. 应用如果想要创建 display link ,需要提供一个目标对 ...

  3. NSTimer 详细设置

    NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483 NSTimer 详细设置2:http://blog.csdn.n ...

  4. 用block解决nstimer循环引用

    大多数开发者可能都会这样来实现定时器.创建定时器的时候,由于目标对象是self,所以要保留此实例.然而,因为定时器是用实例变量存放的,所以实例也保留了定时器,这就造成了循环引用.除非调用stop方法, ...

  5. 解决NSTimer循环引用Retain Cycle问题

    解决NSTimer循环引用Retain Cycle问题 iOS开发中以下的情况会产生循环引用 block delegate NSTimer 循环引用导致一些对象无法销毁,一定的情况下会对我们横须造成影 ...

  6. iOS中NSTimer的invalidate调用之后

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...

  7. NSTimer循环引用的几种解决方案

    前言 在iOS中,NSTimer的使用是非常频繁的,但是NSTimer在使用中需要注意,避免循环引用的问题.之前经常这样写: - (void)setupTimer { self.timer = [NS ...

  8. NSTimer深入理解

    NSTimer,即计时器,用于定时执行一些任务,一次或者多次. 系统Foundation框架提供的最常用方法如下,创建一个NSTimer,并将它放到当前runloop的default mode中. + ...

  9. 解决NSTimer循环引用

    NSTimer常见用法 @interface XXClass : NSObject - (void)start; - (void)stop; @end @implementation XXClass ...

随机推荐

  1. MySQL实战 | 04 为什么要使用索引?

    原文链接:MySQL实战 | 为什么要使用索引? 用过 MySQL 的应该都知道索引是干啥的吧,应该多少都设置过索引,但是若是问你索引是怎么实现的,你能说上来吗? 索引是什么? MySQL 官方对索引 ...

  2. Objective-C中的浅拷贝和深拷贝

    浅拷贝 浅拷贝就是对内存地址的复制,让目标对象指针和源对象指向同一片内存空间.如: char* str = (char*)malloc(100); char* str2 = str; 浅拷贝只是对对象 ...

  3. flask第十七篇——模板【1】

    从这一节开始我们就正式进入flask一个重要的模块——模板了. 我们平时看的知乎平台就是Python开发的,可以看到他的很多页面布局都是一样的,比如你现在搜“如何自学Python”,去知乎看他的页面是 ...

  4. MySQL数据库命名规范及约定

    一.[操作规范]1. 如无备注,则表中的第一个id字段一定是主键且为自动增长:2. 如无备注,则数值类型的字段请使用UNSIGNED属性:3. 如无备注,排序字段order_id在程序中默认使用降序排 ...

  5. 使用vigil 监控微服务系统包含可视化界面

    1. 安装 a. rust cargo cargo install vigil-server b. docker docker pull valeriansaliou/vigil:v1.3.0 2. ...

  6. 织梦DedeCMS模板通用安装方法

    在网络上有很多可以免费下载的DEDECMS模板,下载之后如何安装是很多新手的共同问题.下面将结合我个人使用DEDECMS模板的一些心得,讲一下DEDE模板安装的通用方法. 1.下载一个模板之后要判断一 ...

  7. ORM 查询

    ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...

  8. JLink RTT Client代替printf(IAR测试OK)

    1.打开J-Link安装目录,确保SEGGER目录中有J-Link RTT Client,没有的话必须安装4.9以上版本: 2.打开SEGGER目录下软件SEGGER目录,硬件版本Hardware是8 ...

  9. Codeforces Round #205 (Div. 2)C 选取数列可以选择的数使总数最大——dp

    http://codeforces.com/contest/353/problem/C Codeforces Round #205 (Div. 2)C #include<stdio.h> ...

  10. VS2003在vista/win7下搜索会出现僵死

    1.  VS2003在vista下搜索关键词的时候会出现僵死的问题的解决方案: VS2003快捷方式右击选中属性->兼容性页签 : 选中用兼容模式运行这个程序,下拉框中用windows xp2 ...