? ? ? ? ? ? 手機(jī)中的FPS數(shù)值可以反饋出頁(yè)面的刷新率渔伯,IPhone的FPS數(shù)值是60顶霞;在應(yīng)用中出現(xiàn)卡頓時(shí),F(xiàn)PS數(shù)值就會(huì)降低,所以FPS值可以反饋出APP界面的流暢度选浑。在項(xiàng)目中蓝厌,我們可以可以做一個(gè)控件來(lái)實(shí)時(shí)顯示FPS值來(lái)監(jiān)控,方便我們隊(duì)頁(yè)面做出優(yōu)化古徒。
如何來(lái)實(shí)現(xiàn)這個(gè)工具拓提?我們可以分為下面的步驟:
1、第一步我們需要?jiǎng)?chuàng)建一個(gè)NSObject類隧膘;創(chuàng)建一個(gè)單例
+ (instancetype)shareFPSDisplay;
2代态、第二步是初始化單例
+ (instancetype)shareFPSDisplay {
static FPSDisplay *shareDisplay;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareDisplay = [[FPSDisplay alloc] init];
});
return shareDisplay;
}
3、我們決定用一個(gè)label顯示FPS值疹吃;還有必要成員
@property (strong, nonatomic) UILabel *displayLabel;//顯示
@property (strong, nonatomic) CADisplayLink *link;//CADisplayLink是一個(gè)將定時(shí)器綁定到顯示屏上負(fù)責(zé)垂直同步的類
@property (assign, nonatomic) NSInteger count;//FPS值大小
@property (assign, nonatomic) NSTimeInterval lastTime;//時(shí)間間隔
@property (strong, nonatomic) UIFont *font;
@property (strong, nonatomic) UIFont *subFont;
4蹦疑、我們要初始化這個(gè)label,寫(xiě)一個(gè)函數(shù)使label能夠顯示出FPS數(shù)值萨驶;
- (instancetype)init {
self = [super init];
if (self) {
[self initDisplayLabel];
}
return self;
}
- (void)initDisplayLabel {
//設(shè)置label的大小 ?我們將label放在屏幕右下角位置
CGRect frame = CGRectMake(SCREEN_WIDTH - 100, SCREEN_HEIGHT - 50, 80, 30);
self.displayLabel = [[UILabel alloc] initWithFrame: frame];
self.displayLabel.layer.cornerRadius = 5;
self.displayLabel.clipsToBounds = YES;
self.displayLabel.textAlignment = NSTextAlignmentCenter;
self.displayLabel.userInteractionEnabled = NO;
self.displayLabel.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.700];
_font = [UIFont fontWithName:@"Menlo" size:14];
if (_font) {
_subFont = [UIFont fontWithName:@"Menlo" size:4];
} else {
_font = [UIFont fontWithName:@"Courier" size:14];
_subFont = [UIFont fontWithName:@"Courier" size:4];
}
//
[self initCADisplayLink];
//在每個(gè)頁(yè)面都可以顯示FPS
[[UIApplication sharedApplication].keyWindow addSubview:self.displayLabel];
}
- (void)initCADisplayLink {
self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
[self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)tick:(CADisplayLink *)link {
if (self.lastTime == 0) {? ? ? ? ? //對(duì)LastTime進(jìn)行初始化
self.lastTime = link.timestamp;
return;
}
self.count += 1;? //記錄tick在1秒內(nèi)執(zhí)行的次數(shù)
NSTimeInterval delta = link.timestamp - self.lastTime;? //計(jì)算本次刷新和上次更新FPS的時(shí)間間隔
//大于等于1秒時(shí)必尼,來(lái)計(jì)算FPS
if (delta >= 1) {
self.lastTime = link.timestamp;
float fps = self.count / delta;? ? ? ? // 次數(shù) 除以 時(shí)間 = FPS (次/秒)
self.count = 0;
[self updateDisplayLabelText: fps];//刷新FPS值
}
}
- (void)updateDisplayLabelText: (float) fps {
//實(shí)現(xiàn)更新label上的FPS值
CGFloat progress = fps / 60.0;
UIColor *color = [UIColor colorWithHue:0.27 * (progress - 0.2) saturation:1 brightness:0.9 alpha:1];
self.displayLabel.text = [NSString stringWithFormat:@"%d FPS",(int)round(fps)];
self.displayLabel.textColor = color;
}
- (void)dealloc {
//最后需要注銷一下
[_link invalidate];
}
最后我們?nèi)绾问褂眠@個(gè)FPS監(jiān)控工具呢?在AppDelegate.m文件中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
加上[FPSDisplay shareFPSDisplay];
顯示效果:
這是github傳送門(mén):
https://github.com/1002698389/FPSDisplay/tree/master