NSTimer *timer;
CADisplayLink *caDisplayLink;
int timeCount;
- (void)viewDidLoad {
[super viewDidLoad];
// NSTimer定时器,创建之后就开始执行。适合后台的数据计算
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update:) userInfo:nil repeats:YES];
// CADisplayLink创建之后不会马上执行,需要加入到运行循环中才能执行 默认一秒执行60次
caDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(display:)];
caDisplayLink.frameInterval = 60; // 1秒执行一次
[caDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void)display:(CADisplayLink *)t
{
timeCount++;
NSLog(@"COUNT = %i",timeCount);
if(timeCount>=10)
{
// 销毁定时器
[caDisplayLink invalidate];
caDisplayLink = nil;
}
}
-(void)update:(NSTimer *)t
{
timeCount++;
NSLog(@"COUNT = %i",timeCount);
if(timeCount>=10)
{
// 销毁定时器
[timer invalidate];
timer = nil;
}
}