From 79f3157edfb7652dadcbebb678fe8774d1d67a56 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Tue, 15 Dec 2015 11:04:19 +0800 Subject: [PATCH] add FPSLabel --- Yep.xcodeproj/project.pbxproj | 4 +++ Yep/Views/Labels/FPSLabel.swift | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Yep/Views/Labels/FPSLabel.swift diff --git a/Yep.xcodeproj/project.pbxproj b/Yep.xcodeproj/project.pbxproj index 8b24c3fc..dab817cc 100644 --- a/Yep.xcodeproj/project.pbxproj +++ b/Yep.xcodeproj/project.pbxproj @@ -320,6 +320,7 @@ 50F5AF991BBBD02A0033C9BC /* FeedMediaCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50F5AF971BBBD02A0033C9BC /* FeedMediaCell.swift */; }; 50F5AF9A1BBBD02A0033C9BC /* FeedMediaCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 50F5AF981BBBD02A0033C9BC /* FeedMediaCell.xib */; }; 50FA9A441B661698009BF2A0 /* UIViewController+Yep.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50FA9A431B661698009BF2A0 /* UIViewController+Yep.swift */; }; + 50FD753D1C1FB4AC0050A382 /* FPSLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50FD753C1C1FB4AC0050A382 /* FPSLabel.swift */; }; 6A56328E1BC90A4500397B53 /* DiscoverCardUserCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A56328C1BC90A4500397B53 /* DiscoverCardUserCell.swift */; }; 6A56328F1BC90A4500397B53 /* DiscoverCardUserCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6A56328D1BC90A4500397B53 /* DiscoverCardUserCell.xib */; }; 6A5632931BC911CB00397B53 /* DiscoverNormalUserCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A5632911BC911CB00397B53 /* DiscoverNormalUserCell.swift */; }; @@ -678,6 +679,7 @@ 50F5AF971BBBD02A0033C9BC /* FeedMediaCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FeedMediaCell.swift; path = Views/Cells/FeedMedia/FeedMediaCell.swift; sourceTree = ""; }; 50F5AF981BBBD02A0033C9BC /* FeedMediaCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = FeedMediaCell.xib; path = Views/Cells/FeedMedia/FeedMediaCell.xib; sourceTree = ""; }; 50FA9A431B661698009BF2A0 /* UIViewController+Yep.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "UIViewController+Yep.swift"; path = "Extensions/UIViewController+Yep.swift"; sourceTree = ""; }; + 50FD753C1C1FB4AC0050A382 /* FPSLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = FPSLabel.swift; path = Views/Labels/FPSLabel.swift; sourceTree = ""; }; 6A56328C1BC90A4500397B53 /* DiscoverCardUserCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DiscoverCardUserCell.swift; path = Views/Cells/DiscoverCardUser/DiscoverCardUserCell.swift; sourceTree = ""; }; 6A56328D1BC90A4500397B53 /* DiscoverCardUserCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = DiscoverCardUserCell.xib; path = Views/Cells/DiscoverCardUser/DiscoverCardUserCell.xib; sourceTree = ""; }; 6A5632911BC911CB00397B53 /* DiscoverNormalUserCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = DiscoverNormalUserCell.swift; path = Views/Cells/DiscoverNormalUser/DiscoverNormalUserCell.swift; sourceTree = ""; }; @@ -2154,6 +2156,7 @@ isa = PBXGroup; children = ( 50F2B97A1B2EEAF800F840CC /* NavigationTitleLabel.swift */, + 50FD753C1C1FB4AC0050A382 /* FPSLabel.swift */, ); name = Labels; sourceTree = ""; @@ -2798,6 +2801,7 @@ 50F2B97B1B2EEAF800F840CC /* NavigationTitleLabel.swift in Sources */, 501690231BFEA6C30096C4F9 /* FeedBasicCell.swift in Sources */, 50A820241B969B0F007CD30E /* NSTimeZone+Yep.swift in Sources */, + 50FD753D1C1FB4AC0050A382 /* FPSLabel.swift in Sources */, 504F415A1AD5028200FBB19A /* SkillCell.swift in Sources */, 84385C481B85AC890071130D /* ShowStepMatchViewController.swift in Sources */, 0A0027D61B19CEC000AD4BE0 /* ConversationOperationQueue.swift in Sources */, diff --git a/Yep/Views/Labels/FPSLabel.swift b/Yep/Views/Labels/FPSLabel.swift new file mode 100644 index 00000000..35966187 --- /dev/null +++ b/Yep/Views/Labels/FPSLabel.swift @@ -0,0 +1,57 @@ +// +// FPSLabel.swift +// Yep +// +// Created by nixzhu on 15/12/15. +// Copyright © 2015年 Catch Inc. All rights reserved. +// + +import UIKit + +class FPSLabel: UILabel { + + private var displayLink: CADisplayLink? + private var lastTime: NSTimeInterval = 0 + private var count: Int = 0 + + deinit { + displayLink?.invalidate() + } + + override func didMoveToSuperview() { + super.didMoveToSuperview() + + run() + } + + func run() { + + displayLink = CADisplayLink(target: self, selector: "tick:") + displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) + } + + func tick(displayLink: CADisplayLink) { + + if lastTime == 0 { + lastTime = displayLink.timestamp + return + } + + count += 1 + + let timeDelta = displayLink.timestamp - lastTime + + if timeDelta < 1 { + return + } + + lastTime = displayLink.timestamp + + let fps: Double = Double(count) / timeDelta + + count = 0 + + text = String(format: "%.1f", fps) + } +} +