定时器,循环执行某一任务
看下书上的例子
- (void) paint:(NSTimer *)paramTimer{
/* Do something here */
NSLog(@"Painting");
}
- (void) startPainting{
self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(paint:)
userInfo:nil
repeats:YES];
}
- (void) stopPainting{
if (self.paintingTimer != nil){
[self.paintingTimer invalidate];
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
[self stopPainting];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[self startPainting];
}
输出:
2014-03-12 16:30:58.362 cookbook[710:a0b] Painting
2014-03-12 16:30:59.362 cookbook[710:a0b] Painting
2014-03-12 16:31:00.362 cookbook[710:a0b] Painting
2014-03-12 16:31:01.362 cookbook[710:a0b] Painting
。。。。。。
[self.paintingTimer invalidate]; invalidate方法会释放掉定时器,不需要再手动释放。它不仅会释放定时器本身,还会释放它retain的 userInfo参数对象。
如果repeats值是NO。则定时器在触发后会自己invalidate
还可以用NSInvocation来创建定时器
- (void) startPainting{
/* Here is the selector that we want to call */
SEL selectorToCall = @selector(paint:);
/* Here we compose a method signature out of the selector.
We know that the selector is in the current class so it is easy to construct the method signature */
NSMethodSignature *methodSignature =[[self class] instanceMethodSignatureForSelector:selectorToCall];
/* Now base the invocation on the method signature. We need this invocation to schedule a timer */
NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self];
[invocation setSelector:selectorToCall];
/* Start a scheduled timer now */
self.paintingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
invocation:invocation
repeats:YES];
}
还可以先创建,需要的时候在手动加到Run loop里面去
- (void) startPainting{
self.paintingTimer = [NSTimer timerWithTimeInterval:1.0 target:self
selector:@selector(paint:)
userInfo:nil
repeats:YES];
/* Do your processing here and, whenever you are ready,
use the addTimer:forMode instance method of the NSRunLoop class in order to schedule the timer on that run loop */
[[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
}
当然了,也可以NSInvocation加手动
- (void) startPainting{
/* Here is the selector that we want to call */
SEL selectorToCall = @selector(paint:);
/* Here we compose a method signature out of the selector. We
know that the selector is in the current class so it is easy
to construct the method signature */
NSMethodSignature *methodSignature =[[self class] instanceMethodSignatureForSelector:selectorToCall];
/* Now base the invocation on the method signature. We need this invocation to schedule a timer */
NSInvocation *invocation =[NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:self];
[invocation setSelector:selectorToCall];
self.paintingTimer = [NSTimer timerWithTimeInterval:1.0
invocation:invocation
repeats:YES];;
/* Do your processing here and, whenever you are ready,
use the addTimer:forMode instance method of the NSRunLoop class in order to schedule the timer on that run loop */
[[NSRunLoop currentRunLoop] addTimer:self.paintingTimer forMode:NSDefaultRunLoopMode];
}