NSTimer其实是将一个监听加入的系统的RunLoop中去,当系统runloop到符合timer条件的循环时,会调用timer一次,当timer执行完,也就是回调函数执行之后,timer会再一次的将自己加入到runloop中去继续监听。
CFRunLoopTimerRef 和 NSTimer这两个类型是可以互换的, 当我们在传参数的时候,看到CFRunLoopTimerRef可以传NSTimer的参数,增加强制转化来避免编译器的警告信息
指定(注册)一个timer到 RunLoops中
在同一时间一个timer对象只能够被注册到一个runloop中,尽管在这个runloop中它能够被添加到多个runloop的模式中去。
有以下三种方法:
使用 scheduledTimerWithTimeInterval:invocation:repeats: 或者scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: 这两个类方法创建一个timer并把它指定到一个默认的runloop模式中
使用 timerWithTimeInterval:invocation:repeats: 或者 timerWithTimeInterval:target:selector:userInfo:repeats:这两个类方法创建一个timer的对象,不把它指定到run loop. (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它制定到一个runloop模式中.)
使用 initWithFireDate:interval:target:selector:userInfo:repeats: 方法分配并创建一个NSTimer的实例 (当创建之后,你必须手动的调用NSRunLoop下对应的方法 addTimer:forMode: 去将它指定到一个runloop模式中.)
- // 安装timer(注册timer)
- NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 5// 当函数正在调用时,及时间隔时间到了 也会忽略此次调用
- target: self
- selector: @selector(handleTimer:)
- userInfo: nil
- repeats: YES]; // 如果是NO 不重复,则timer在触发了回调函数调用完成之后 会自动释放这个timer,以免timer被再一次的调用,如果是YES,则会重复调用函数,调用完函数之后,会将这个timer加到RunLoop中去,等待下一次的调用,直到用户手动释放timer( [timer invalidate];)。
- - (void) handleTimer: (NSTimer *) timer // timer的回调函数
- {
- //在这里进行处理
- NSLog(@"1");
- // for (int i = 0; i <= 1000000000; )
- // {
- // i++;
- // }
- }
- // [timer invalidate]; // 这个函数将timer从当前的RunLoop中remove掉,必须在timer安装的线程中调用这个函数。
- [timer fire];// 可以通过fire这个方法去触发timer,即使timer的firing time没有到达
创建一个 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];
}
}