ios查看帧率的软件_查看iOS屏幕帧数MGFPSStatus

在状态栏显示FPS状态,FPS是一秒钟渲染多少帧 Frame Per Second = FPS,FPS值为55~60最佳,低于这个范围就较为卡顿了。

使用方法

#if defined(DEBUG) || defined(_DEBUG)

// 添加FPSStatus

[[MGFPSStatus shareInstance] show];

#endif

状态栏MGFPSStatus

FPSStatus.m

@implementation MGFPSStatus {

// 顶部FPSLabel

UILabel *_fpsLabel;

CADisplayLink *_displayLink;

NSTimeInterval _lastTime;

NSUInteger _count;

UIColor *_fpsColor;

}

+ (instancetype)shareInstance

{

static MGFPSStatus *_instance;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_instance = [[MGFPSStatus alloc] init];

});

return _instance;

}

- (instancetype)init

{

self = [super init];

if (self) {

// 创建label

_fpsLabel = [[UILabel alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width)/2+30, 0, 50, 20)];

// _fpsLabel.text = @"23344";

_fpsLabel.font = [UIFont systemFontOfSize:12];

_fpsLabel.layer.cornerRadius = 5;

_fpsLabel.clipsToBounds = YES;

_fpsLabel.textAlignment = NSTextAlignmentCenter;

_fpsLabel.userInteractionEnabled = NO;

_fpsLabel.backgroundColor = [UIColor colorWithWhite:1.000 alpha:0.700];

// 创建定时器

_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];

[_displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

return self;

}

- (void)tick:(CADisplayLink *)link {

// 第一次

if (_lastTime == 0) {

_lastTime = link.timestamp;

return;

}

_count++;

NSTimeInterval delta = link.timestamp - _lastTime;

if (delta < 1) return;

_lastTime = link.timestamp;

float fps = _count / delta;

_count = 0;

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%d FPS",(int)round(fps)]];

// 根据卡顿程度显示颜色

if (fps >= 55.0) {

_fpsColor = [UIColor greenColor];

} else if (fps>=50 && fps<55) {

_fpsColor = [UIColor yellowColor];

} else {

_fpsColor = [UIColor redColor];

}

[text addAttribute:NSForegroundColorAttributeName value:_fpsColor range:NSMakeRange(0, text.length - 3)];

[text addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(text.length - 3, 3)];

_fpsLabel.attributedText = text;

}

- (void)dealloc

{

[_displayLink invalidate];

NSLog(@"release");

}

- (void)show {

[[UIApplication sharedApplication].keyWindow addSubview:_fpsLabel];

}

@end

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值