iOS
手机中的FPS数值可以反馈出页面的刷新率,IPhone的FPS数值是60;在应用中出现卡顿时,FPS数值就会降低,所以FPS值可以反馈出APP界面的流畅度。在项目中,我们可以可以做一个控件来实时显示FPS值来监控,方便我们队页面做出优化。
如何来实现这个工具?我们可以分为下面的步骤:
1、第一步我们需要创建一个NSObject类;创建一个单例
+ (instancetype)shareFPSDisplay;
2、第二步是初始化单例
+ (instancetype)shareFPSDisplay {
static FPSDisplay *shareDisplay;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shareDisplay = [[FPSDisplay alloc] init];
});
return shareDisplay;
}
3、我们决定用一个label显示FPS值;还有必要成员
@property (strong, nonatomic) UILabel *displayLabel;//显示
@property (strong, nonatomic) CADisplayLink *link;//CADisplayLink是一个将定时器绑定到显示屏上负责垂直同步的类
@property (assign, nonatomic) NSInteger count;//FPS值大小
@property (assign, nonatomic) NSTimeInterval lastTime;//时间间隔
@property (strong, nonatomic) UIFont *font;
@property (strong, nonatomic) UIFont *subFont;
4、我们要初始化这个label,写一个函数使label能够显示出FPS数值;
- (instancetype)init {
self = [super init];
if (self) {
[self initDisplayLabel];
}
return self;
}
- (void)initDisplayLabel {
//设置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];
//在每个页面都可以显示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) { //对LastTime进行初始化
self.lastTime = link.timestamp;
return;
}
self.count += 1; //记录tick在1秒内执行的次数
NSTimeInterval delta = link.timestamp - self.lastTime; //计算本次刷新和上次更新FPS的时间间隔
//大于等于1秒时,来计算FPS
if (delta >= 1) {
self.lastTime = link.timestamp;
float fps = self.count / delta; // 次数 除以 时间 = FPS (次/秒)
self.count = 0;
[self updateDisplayLabelText: fps];//刷新FPS值
}
}
- (void)updateDisplayLabelText: (float) fps {
//实现更新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];
}
最后我们如何使用这个FPS监控工具呢?在AppDelegate.m文件中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
加上[FPSDisplay shareFPSDisplay];
显示效果:
这是github传送门:
https://github.com/1002698389/FPSDisplay/tree/master