YYModel學(xué)習(xí)筆記之FPSLabel查看屏幕幀數(shù)

鎮(zhèn)樓圖.JPG

前言

前段時(shí)間在研究YYModel框架時(shí)學(xué)到不少東西,尤其是下面要講述的查看屏幕幀數(shù)的小工具.個(gè)人感覺在項(xiàng)目里比較實(shí)用,自己模仿寫了一個(gè),代碼中有詳細(xì)注釋,有需要的小伙伴們可以點(diǎn)擊這里查看, 如果star一下就更好了??, 請(qǐng)看我的效果圖??

fps.gif

AxeFPSLabel實(shí)現(xiàn)思路

  • 首先說下CADisplayLink, CADisplayLink是一種定時(shí)器, 系統(tǒng)的每一幀刷新時(shí)都會(huì)被調(diào)用. CADisplayLink中的timestamp是系統(tǒng)每次調(diào)用時(shí)的系統(tǒng)時(shí)間戳,通過計(jì)算兩次時(shí)間戳的間隔,可以得到每一幀所花費(fèi)的時(shí)間,既可以獲取當(dāng)前每秒能刷新多少幀。

下面給出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,都不能解決循環(huán)引用問題
    _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;
}

后續(xù)

  • 閑下來的時(shí)候會(huì)繼續(xù)完善補(bǔ)充文章,各位小伙伴有什么見解或是疑問可以留言給我,歡迎討論??!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,251評(píng)論 25 708
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 11,145評(píng)論 6 13
  • 作為一個(gè)展覽設(shè)計(jì)師不只是會(huì)畫個(gè)圖,熟練掌握設(shè)計(jì)軟件就OK了, 每個(gè)展位都是一個(gè)企業(yè)的濃縮,要了解客戶所屬行業(yè)形勢(shì),...
    四色堇閱讀 897評(píng)論 0 0
  • 心里憋著一股勁,想哭卻哭不出來,30歲仍像個(gè)小孩子,昨天剛離開家,今天就想爸爸媽媽和姐姐了,太過于感性的人或者真的...
    上帝和你WJ閱讀 192評(píng)論 0 0
  • 我今天終于穿兩件衣服啦!哈哈哈!氣溫下降啦!朋友們注意保暖哦!
    王利河閱讀 220評(píng)論 0 0