NSTimer 可以精确控制50 - 100ms 的时间,如何Timer 处理函数比较耗时,真实的间隔时间可以比预设时间长,core function framework 也提供相似的对象, CFRunLoopTimerRef, 而且CFRunLoopTimerRef 可以 和NSTimer 相互替换
NSTimer class
Creating Timer
1. scheduledTimerWithTimeInterval:invocation:repeats:
>> 最短间隔时间0.1 millisecond
2. scheduledTimerWithTimeInterval:target:selector:userInfo:repeats
3. timerWithTimerInterval:invocation:repeats:
4. timerWithTimerInterval:target:selector:userInfo:repeats:
5. initWithFireDate:interval:target:selector:userInfo:repeats:
Firing a Timer
1. fire
Stopping a Timer
1. invalidate
Information for Timer
1. isValid
2. fireDate
3. setFireDate
4. timeInterval
5. userInfo
example:
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(func:) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(func:) userInfo:nil repeats:YES];
//stop timer
[myTimer invalidate];
myTimer = nil;
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(TimeElapse:) userInfo:nil repeats:YES];
-(void) TimeElapse:(id) sender
{
//do something here
}