美文网首页
swift-测试帧数

swift-测试帧数

作者: Abler | 来源:发表于2016-08-15 18:37 被阅读101次

    测试iphone的帧数,笔者练习swift写的,

    [源码demo](https://github.com/AblerSong/FPSDemo)
    import UIKit
    
    class FPSLabel: UILabel {
    
        private var link:CADisplayLink?
        
        private var lastTime:NSTimeInterval = 0.0;
        
        private var count:Int = 0;
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            
        
            link = CADisplayLink.init(target: self, selector: #selector(FPSLabel.didTick(_:)))
            link?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
            
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        deinit{
            link?.invalidate()
        }
        
        func didTick(link:CADisplayLink){
        
            if lastTime == 0 {
                lastTime = link.timestamp
                return
            }
            count += 1
            
            let delta = link.timestamp - lastTime
            
            if delta < 1 {
                return
            }
            
            lastTime = link.timestamp
            
            // 帧数========>可以自己定义作为label显示
            let fps = Double(count) / delta
            
            
            count = 0
            
            text = String(format: "%02.0f帧",round(fps))
            
            // 打印帧数
            print(text ?? "0")
            
        }
    }
    

    相关文章

      网友评论

          本文标题:swift-测试帧数

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