swift-测试帧数
2016-08-15 本文已影响101人
Abler
[源码demo](https://github.com/AblerSong/FPSDemo)测试iphone的帧数,笔者练习swift写的,
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")
}
}