[[CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction:)] addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
- (void)displayLinkAction:(CADisplayLink *)link { static NSTimeInterval lastTime = 0; static NSInteger frameCount = 0; if (lastTime == 0) { lastTime = link.timestamp; return; } frameCount ++; NSTimeInterval paseTime = link.timestamp - lastTime; if (paseTime > 1) { NSInteger fps = frameCount / paseTime; lastTime = link.timestamp; frameCount = 0; NSLog(@"%ld",fps); } }
原理:
由于iOS设备每加载一帧都会调用 displayLinkAction 函数
设置全局变量,求1s内加载了多少帧,然后除以第一帧和最后一帧的时间间隔。