From 443488bf0d747cd4eca41ed69027396485b6821a Mon Sep 17 00:00:00 2001 From: Kirill Serebriakov Date: Wed, 29 May 2019 20:47:47 +0300 Subject: [PATCH] - updated frame rate on fly - added frame rate change test to example --- EFCountingLabel/Classes/EFCount.swift | 7 +- .../EFCountingLabel.xcodeproj/project.pbxproj | 4 + .../Base.lproj/Main.storyboard | 114 ++++++++++++++++-- .../DynamicFrameRateViewController.swift | 56 +++++++++ .../StoryboardButtonViewController.swift | 2 - 5 files changed, 172 insertions(+), 11 deletions(-) create mode 100644 Example/EFCountingLabel/ViewControllers/DynamicFrameRateViewController.swift diff --git a/EFCountingLabel/Classes/EFCount.swift b/EFCountingLabel/Classes/EFCount.swift index 9cf7ef6..2a5f8b2 100644 --- a/EFCountingLabel/Classes/EFCount.swift +++ b/EFCountingLabel/Classes/EFCount.swift @@ -51,7 +51,12 @@ public class EFCounter { public var timingFunction: EFTiming = EFTimingFunction.linear //works same way as CADisplayLink.frameInterval //0 - max frame rate - public var refreshRateInterval: Int = 2 + public var refreshRateInterval: Int = 2 { + didSet { + guard let timer = timer else { return } + apply(interval: refreshRateInterval, to: timer) + } + } public var updateBlock: ((CGFloat) -> Void)? public var completionBlock: (() -> Void)? diff --git a/Example/EFCountingLabel.xcodeproj/project.pbxproj b/Example/EFCountingLabel.xcodeproj/project.pbxproj index dce2686..6b1c30b 100644 --- a/Example/EFCountingLabel.xcodeproj/project.pbxproj +++ b/Example/EFCountingLabel.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; }; 615470077217336459F28548 /* StoryboardButtonViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615478D55F97750BE62D6350 /* StoryboardButtonViewController.swift */; }; 61547605835C3408C93468DF /* CustomTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615478D4657C10BDDB2930B2 /* CustomTableViewController.swift */; }; + 6154767B05F9D92C4A3CCD3D /* DynamicFrameRateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6154777F75D51F92326A369C /* DynamicFrameRateViewController.swift */; }; 61547AB8DD5A704F70185C87 /* ListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6154772B1F9DBE58D87528C6 /* ListTableViewController.swift */; }; 61547FDE291FDA37914D8D74 /* StoryboardLabelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615477C97BA7D1BE03393744 /* StoryboardLabelViewController.swift */; }; 88B4CB65271A07819AE9D976 /* Pods_EFCountingLabel_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FF702AD45972585A86A336D5 /* Pods_EFCountingLabel_Example.framework */; }; @@ -48,6 +49,7 @@ 607FACEA1AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = ""; }; 6154772B1F9DBE58D87528C6 /* ListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListTableViewController.swift; sourceTree = ""; }; + 6154777F75D51F92326A369C /* DynamicFrameRateViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicFrameRateViewController.swift; sourceTree = ""; }; 615477C97BA7D1BE03393744 /* StoryboardLabelViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardLabelViewController.swift; sourceTree = ""; }; 615478D4657C10BDDB2930B2 /* CustomTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTableViewController.swift; sourceTree = ""; }; 615478D55F97750BE62D6350 /* StoryboardButtonViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardButtonViewController.swift; sourceTree = ""; }; @@ -159,6 +161,7 @@ 615477C97BA7D1BE03393744 /* StoryboardLabelViewController.swift */, 615478D4657C10BDDB2930B2 /* CustomTableViewController.swift */, 29AF0090229EE2CE006A18B4 /* FrameRateViewController.swift */, + 6154777F75D51F92326A369C /* DynamicFrameRateViewController.swift */, ); path = ViewControllers; sourceTree = ""; @@ -352,6 +355,7 @@ 615470077217336459F28548 /* StoryboardButtonViewController.swift in Sources */, 61547FDE291FDA37914D8D74 /* StoryboardLabelViewController.swift in Sources */, 61547605835C3408C93468DF /* CustomTableViewController.swift in Sources */, + 6154767B05F9D92C4A3CCD3D /* DynamicFrameRateViewController.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Example/EFCountingLabel/Base.lproj/Main.storyboard b/Example/EFCountingLabel/Base.lproj/Main.storyboard index cbb47e1..2ce5e87 100644 --- a/Example/EFCountingLabel/Base.lproj/Main.storyboard +++ b/Example/EFCountingLabel/Base.lproj/Main.storyboard @@ -61,7 +61,85 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -131,7 +209,7 @@ - + @@ -184,9 +262,29 @@ - + + + + + + + + + + + + + + + @@ -209,7 +307,7 @@ - + @@ -226,7 +324,7 @@ - + @@ -247,7 +345,7 @@ - + @@ -349,7 +447,7 @@ - + @@ -452,7 +550,7 @@ - + diff --git a/Example/EFCountingLabel/ViewControllers/DynamicFrameRateViewController.swift b/Example/EFCountingLabel/ViewControllers/DynamicFrameRateViewController.swift new file mode 100644 index 0000000..4536603 --- /dev/null +++ b/Example/EFCountingLabel/ViewControllers/DynamicFrameRateViewController.swift @@ -0,0 +1,56 @@ +// +// Created by Kirow Onet on 2019-05-29. +// Copyright (c) 2019 CocoaPods. All rights reserved. +// + +import Foundation +import UIKit +import EFCountingLabel + +class DynamicFrameRateViewController: UIViewController { + @IBOutlet weak var countingLabel: EFCountingLabel! + @IBOutlet weak var frameIntervalSlider: UISlider! + @IBOutlet weak var expectedFrameRate: UILabel! + @IBOutlet weak var actualFrameRate: UILabel! + + var eventCount = 0 + + var timer: Timer? + + override func viewDidLoad() { + super.viewDidLoad() + countingLabel.setUpdateBlock { [unowned self] value, label in + self.eventCount += 1 + label.text = "\(Int(value.truncatingRemainder(dividingBy: 60)))" + } + frameIntervalSlider.value = Float(countingLabel.counter.refreshRateInterval) + update(interval: countingLabel.counter.refreshRateInterval) + countingLabel.countFrom(0, to: 3_600_000, withDuration: 3600) + } + + @IBAction func sliderDidChangeValue(_ sender: UISlider) { + update(interval: Int(sender.value)) + + } + + private func update(interval: Int) { + eventCount = 0 + countingLabel.counter.refreshRateInterval = interval + timer?.invalidate() + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(refreshActualFramerate), + userInfo: nil, repeats: true) + expectedFrameRate.text = interval > 0 ? "\(60 / interval)" : "max" + actualFrameRate.text = "0" + } + + @objc func refreshActualFramerate() { + actualFrameRate.text = "\(eventCount)" + eventCount = 0 + } + + deinit { + timer?.invalidate() + timer = nil + } + +} \ No newline at end of file diff --git a/Example/EFCountingLabel/ViewControllers/StoryboardButtonViewController.swift b/Example/EFCountingLabel/ViewControllers/StoryboardButtonViewController.swift index c4e8ce6..d890923 100644 --- a/Example/EFCountingLabel/ViewControllers/StoryboardButtonViewController.swift +++ b/Example/EFCountingLabel/ViewControllers/StoryboardButtonViewController.swift @@ -63,8 +63,6 @@ class StoryboardButtonViewController: UIViewController { } else { sender.contentHorizontalAlignment = .left sender.countFromCurrentValueTo(1000000, withDuration: 20) - } - } }