美文网首页iOS Developer程序员
YYModel学习笔记之FPSLabel查看屏幕帧数

YYModel学习笔记之FPSLabel查看屏幕帧数

作者: GiantAxe77 | 来源:发表于2016-12-15 14:01 被阅读174次
    镇楼图.JPG

    前言

    前段时间在研究YYModel框架时学到不少东西,尤其是下面要讲述的查看屏幕帧数的小工具.个人感觉在项目里比较实用,自己模仿写了一个,代码中有详细注释,有需要的小伙伴们可以点击这里查看, 如果star一下就更好了😆, 请看我的效果图👇

    fps.gif

    AxeFPSLabel实现思路

    • 首先说下CADisplayLink, CADisplayLink是一种定时器, 系统的每一帧刷新时都会被调用. CADisplayLink中的timestamp是系统每次调用时的系统时间戳,通过计算两次时间戳的间隔,可以得到每一帧所花费的时间,既可以获取当前每秒能刷新多少帧。

    下面给出YYFPSLabel源码

    • YYFPSLabel.h
    /**
     Show Screen FPS...
     
     The maximum fps in OSX/iOS Simulator is 60.00.
     The maximum fps on iPhone is 59.97.
     The maxmium fps on iPad is 60.0.
     */
    @interface YYFPSLabel : UILabel
    
    @end
    
    • YYFPSLabel.m
    #import "YYFPSLabel.h"
    #import "YYWeakProxy.h"
    
    #define kSize CGSizeMake(55, 20)
    
    @implementation YYFPSLabel {
        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];
        }
        
        // 如果直接用 self 或者 weakSelf,都不能解决循环引用问题
        _link = [CADisplayLink displayLinkWithTarget:[YYWeakProxy proxyWithTarget:self] selector:@selector(tick:)];
    //    __weak typeof(self) weakSelf = self;
    //    _link = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(tick:)];
        [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
        return self;
    }
    
    - (void)dealloc {
        [_link invalidate];
        NSLog(@"timer release");
    }
    
    - (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:@"%d FPS",(int)round(fps)]];
        [text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, text.length - 3)];
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(text.length - 3, 3)];
        [text addAttribute:NSFontAttributeName value:_font range:NSMakeRange(0, text.length)];
        [text addAttribute:NSFontAttributeName value:_subFont range:NSMakeRange(text.length - 4, 1)];
        self.attributedText = text;
    }
    

    后续

    • 闲下来的时候会继续完善补充文章,各位小伙伴有什么见解或是疑问可以留言给我,欢迎讨论😁!

    相关文章

      网友评论

        本文标题:YYModel学习笔记之FPSLabel查看屏幕帧数

        本文链接:https://www.haomeiwen.com/subject/ynnimttx.html