iOS 测试界面流畅度

从别人的demo里扒拉出来的一段代码,可以检测当前界面的帧流率,对于界面流畅度有很高要求的情况下可以用该段代码做检测

使用的时候直接创建该Label,然后给一个frame即可,很方便.

// .h文件
#import <UIKit/UIKit.h>

@interface GGFPSLabel : UILabel

@end

// .m文件
#import "GGFPSLabel.h"

#define kSize CGSizeMake(100, 20)

/*** 帧率 */
@implementation GGFPSLabel{
    CADisplayLink *_link;
    NSUInteger _count;
    NSTimeInterval _lastTime;
    UIFont *_font;
    UIFont *_subFont;
    
    NSTimeInterval _llll;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size = kSize;
        
    }
    self = [super initWithFrame:frame];
    
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
    self.textAlignment = NSTextAlignmentCenter;
    self.userInteractionEnabled = NO;
    self.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];
    }
    
    _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
    [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    return self;
}

- (void)dealloc {
    [_link invalidate];
}

- (CGSize)sizeThatFits:(CGSize)size {
    return kSize;
}

- (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;
    
    CGFloat progress = fps / 60.0;
    UIColor *color = [UIColor colorWithHue:0.27 * (progress - 0.2) saturation:1 brightness:0.9 alpha:1];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f FPS", fps]];
    [text setAttributes:@{NSForegroundColorAttributeName : color} range:NSMakeRange(0, text.length - 3)];
    [text setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(text.length - 3, 3)];
    self.attributedText = text;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 2017届全国普通高校毕业生预计795万人。面对现在没有以往的“包分配”政策,大学生结合自身所学初涉工作岗位的基本...
    回忆之美hyzm阅读 426评论 0 1
  • 上学期,我有一门策划课。结课作业是一份房地产的策划案。一般这种课会在配以一定实验课的课时,方便我们跟着老师的进度一...
    多笑笑_阅读 501评论 0 2
  • 瘦人碗子阅读 150评论 0 1
  • 读完这本书,像是在旁观几个人的人生。 说说里面让我印象深刻的片段吧。 A.J.意识到自己爱上阿米莉亚时,开始表现的...
    CJJShare阅读 457评论 1 1