//
// DHFPSLabel.swift
// bd
//
// Created by qhhuang on 2017/10/25.
// Copyright © 2017年 qhhuang. All rights reserved.
//
import UIKit
//import YYKit.NSA
class DHFPSLabel: UILabel {
lazy var link: CADisplayLink = {
let link = CADisplayLink(target: self, selector: #selector(DHFPSLabel.tick))
return link
}()
var count: Int = 0
lazy var lastTime: TimeInterval = TimeInterval()
lazy var mainfont: UIFont = UIFont(name: "Menlo", size: 14)!
lazy var subFont: UIFont = UIFont(name: "Menlo", size: 5)!
lazy var llll: TimeInterval = TimeInterval()
override init(frame: CGRect) {
super.init(frame: frame)
if frame.size.width == 0 && frame.size.height == 0 {
//frame.size = CGSize.init(width: 55, height: 20)
}
self.layer.cornerRadius = 5
self.clipsToBounds = true
self.textAlignment = NSTextAlignment.center
self.isUserInteractionEnabled = false
self.backgroundColor = UIColor.init(white: 0.0, alpha: 0.7)
link.add(to: RunLoop.current, forMode: .commonModes)
}
@objc func tick(link: CADisplayLink) {
if lastTime == 0 {
lastTime = link.timestamp
return
}
print("timeStamp\(link.timestamp)")
count = count + 1
let delta = link.timestamp - lastTime
if delta < 1 {
return
}
lastTime = link.timestamp
let fps:CGFloat = CGFloat(count) / CGFloat(delta)
count = 0
let progress = fps / 60.0
let color = UIColor.init(hue: 0.27 * (progress - 0.2), saturation: 1, brightness: 0.9, alpha: 1)
let attrString = NSMutableAttributedString(string: "\(Int(fps)) FPS")
attrString.addAttributes([NSAttributedStringKey.foregroundColor: color], range: NSRange(location: 0, length: attrString.length - 3))
attrString.addAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], range: NSRange(location: attrString.length - 3, length: 3))
attrString.addAttributes([NSAttributedStringKey.font: mainfont], range: NSRange.init(location: 0, length: attrString.length))
attrString.addAttributes([NSAttributedStringKey.font: subFont], range: NSRange.init(location: attrString.length - 4, length: 1))
self.attributedText = attrString;
}
deinit {
link.invalidate()
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return CGSize.init(width: 50, height: 20)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
swift PFSLabel
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近升级Xcode8后运行提示Swift版本错误,具体错误如下: “Use Legacy Swift Langua...
- 背景:在项目中引入了Alamofire第三方库,用 Xcode9 打开了一次项目。再用Xcode8打开报错:Mod...
- 1、oc工程调用swift-----自动创建桥接头文件 创建一个oc工程: 设置Product Module Na...
- OC中使用Swift 1、在项目中右键添加一个 Swift 文件,如TestSwift.swift。 2、添加后 ...
- 创建对象 OC:alloc initWith Swift:(XXX:) 调用方法 OC:[uicolor redc...