forked from Zero-zhou/iOS-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPSLabel.swift
76 lines (56 loc) · 2 KB
/
FPSLabel.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// FPSLabel.swift
//
// Created by Augus on 4/13/16.
// Copyright © 2016 iAugus. All rights reserved.
//
import UIKit
//REFERENCE: https://github.com/ibireme/YYText/blob/master/Demo/YYTextDemo/YYFPSLabel.m
class FPSLabel: UILabel {
private var link: CADisplayLink!
private lazy var count: Int = 0
private lazy var lastTime: TimeInterval = 0
override init(frame: CGRect) {
super.init(frame: frame)
center = CGPoint(x: 18, y: 23)
if frame != .zero {
self.frame = frame
}
layer.cornerRadius = 5
clipsToBounds = true
textAlignment = .center
textColor = UIColor.white
backgroundColor = UIColor(white: 0, alpha: 0.7)
font = UIFont(name: "HelveticaNeue", size: 14)
isUserInteractionEnabled = false
weak var weakSelf = self
link = CADisplayLink(target: weakSelf!, selector:#selector(FPSLabel.tick(_:)) );
link.add(to: RunLoop .main, forMode:RunLoopMode.commonModes)
}
convenience init(center: CGPoint) {
self.init()
self.center = center
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
sizeToFit()
}
@objc private func tick(_ link: CADisplayLink) {
if lastTime == 0 {
lastTime = link.timestamp
return
}
count += 1
let delta = link.timestamp - lastTime
guard delta >= 1 else { return }
lastTime = link.timestamp
let fps = Double(count) / delta
count = 0
let progress = fps / 60.0;
textColor = UIColor(hue: CGFloat(0.27 * ( progress - 0.2 )) , saturation: 1, brightness: 0.9, alpha: 1)
text = "\(Int(fps + 0.5)) FPS "
}
}