美文网首页iOS基础
iOS 监控屏幕刷新帧率

iOS 监控屏幕刷新帧率

作者: 六弦琴殇 | 来源:发表于2017-05-13 21:31 被阅读217次
    关于CADisplayLink

    A CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.这是官方文档对于CADisplayLink的定义,意思就是说CADisplayLink是一个定时器(但并不继承自NSTimer),它能够让你的应用以屏幕刷新的帧率来同步更新UI,所以我们可以通过它1s内调用指定target的指定方法的次数来获得屏幕刷新的帧率,从而来判断CPU的运行状态。

    使用

       - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [CPUFpsLabel setupOnView:[[application windows] firstObject].rootViewController.view];
        return YES;
    }
    

    CPUFpsLabel

    @interface CPUFpsLabel : UILabel
    + (void)setupOnView:(UIView *)view;
    - (void)start;
    @end
    
    @implementation CPUFpsLabel
    {
        CADisplayLink *_link;
        NSUInteger _count;
        NSTimeInterval _lastTime;
    }
    
    + (void)setupOnView:(UIView *)view
    {
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        CPUFpsLabel *fpsLabel = [[CPUFpsLabel alloc] initWithFrame:CGRectMake(screenSize.width / 2.0 + 25, 0, 50, 20)];
        [fpsLabel start];
        [view addSubview:fpsLabel];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self)
        {
            self.font=[UIFont boldSystemFontOfSize:12];
            self.textColor=[UIColor colorWithRed:0.33 green:0.84 blue:0.43 alpha:1.00];
            self.backgroundColor=[UIColor clearColor];
            self.textAlignment = NSTextAlignmentCenter;
            
            _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
            [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
            _link.paused = YES;
            
            
            [[NSNotificationCenter defaultCenter] addObserver: self
                                                     selector: @selector(applicationDidBecomeActiveNotification)
                                                         name: UIApplicationDidBecomeActiveNotification
                                                       object: nil];
            
            [[NSNotificationCenter defaultCenter] addObserver: self
                                                     selector: @selector(applicationWillResignActiveNotification)
                                                         name: UIApplicationWillResignActiveNotification
                                                       object: nil];
        }
        return self;
    }
    
    - (void)dealloc
    {
        [_link invalidate];
        [_link removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
    
    - (void)applicationDidBecomeActiveNotification
    {
        [_link setPaused:NO];
    }
    
    - (void)applicationWillResignActiveNotification
    {
        [_link setPaused:YES];
    }
    
    - (void)start
    {
        [_link setPaused:NO];
    }
    
    - (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;
        
        NSString *fpsString = [NSString stringWithFormat:@"%zd fps",(int)round(fps)];
        self.text = fpsString;
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:iOS 监控屏幕刷新帧率

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