Guest User

Untitled

a guest
Nov 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. //
  2. // FPSLabel.swift
  3. // SAC
  4. //
  5. // Created by SAGESSE on 2/1/16.
  6. // Copyright © 2016-2017 Sagesse. All rights reserved.
  7. //
  8. // Reference: ibireme/YYKit/YYFPSLabel
  9. //
  10.  
  11. import UIKit
  12.  
  13. ///
  14. /// Show Screen FPS.
  15. ///
  16. /// The maximum fps in OSX/iOS Simulator is 60.00.
  17. /// The maximum fps on iPhone is 59.97.
  18. /// The maxmium fps on iPad is 60.0.
  19. ///
  20. public class FPSLabel: UILabel {
  21.  
  22. public override init(frame: CGRect) {
  23. super.init(frame: frame)
  24. build()
  25. }
  26. public required init?(coder aDecoder: NSCoder) {
  27. super.init(coder: aDecoder)
  28. build()
  29. }
  30.  
  31. public override var intrinsicContentSize: CGSize {
  32. return CGSize(width: 55, height: 20)
  33. }
  34. public override func willMove(toWindow newWindow: UIWindow?) {
  35. super.willMove(toWindow: newWindow)
  36. if newWindow == nil {
  37. _link.invalidate()
  38. } else {
  39. _link.add(to: RunLoop.main, forMode: RunLoopMode.commonModes)
  40. }
  41. }
  42.  
  43. private func build() {
  44.  
  45. text = "calc..."
  46. font = UIFont.systemFont(ofSize: 14)
  47. textColor = UIColor.white
  48. textAlignment = .center
  49. backgroundColor = UIColor(white: 0, alpha: 0.7)
  50.  
  51. layer.cornerRadius = 5
  52. layer.masksToBounds = true
  53. }
  54.  
  55. @objc private func tack(_ link: CADisplayLink) {
  56. guard let lastTime = _lastTime else {
  57. _lastTime = link.timestamp
  58. return
  59. }
  60. _count += 1
  61. let delta = link.timestamp - lastTime
  62. guard delta >= 1 else {
  63. return
  64. }
  65. let fps = Double(_count) / delta + 0.03
  66. let progress = CGFloat(fps / 60)
  67. let color = UIColor(hue: 0.27 * (progress - 0.2), saturation: 1, brightness: 0.9, alpha: 1)
  68.  
  69. let text = NSMutableAttributedString(string: "\(Int(fps)) FPS")
  70. text.addAttribute(NSAttributedStringKey.foregroundColor, value: color, range: NSMakeRange(0, text.length - 3))
  71. attributedText = text
  72.  
  73. _count = 0
  74. _lastTime = link.timestamp
  75. }
  76.  
  77. private var _count: Int = 0
  78. private var _lastTime: TimeInterval?
  79.  
  80. private lazy var _link: CADisplayLink = {
  81. return CADisplayLink(target: self, selector: #selector(tack(_:)))
  82. }()
  83. }
Add Comment
Please, Sign In to add comment