Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked module #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions EFCountingLabel/Classes/EFCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ extension EFCount {
}

public class EFCounter {

public var timingFunction: EFTiming = EFTimingFunction.linear
//works same way as CADisplayLink.frameInterval
//0 - max frame rate
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)?
Expand Down Expand Up @@ -107,6 +116,16 @@ public class EFCounter {
}
}

private func apply(interval: Int, to displayLink: CADisplayLink) {
if #available(iOS 10.3, *) {
displayLink.preferredFramesPerSecond = interval > 0 ? max(1, UIScreen.main.maximumFramesPerSecond / interval) : 0
} else if #available(iOS 10.0, *) {
displayLink.preferredFramesPerSecond = interval > 0 ? max(1, 60 / interval) : 0
} else {
displayLink.frameInterval = interval == 0 ? 1 : interval
}
}

//set init values
public func reset() {
invalidate()
Expand Down Expand Up @@ -147,11 +166,7 @@ extension EFCounter: EFCount {
lastUpdate = CACurrentMediaTime()

let timer = CADisplayLink(target: self, selector: #selector(updateValue(_:)))
if #available(iOS 10.0, *) {
timer.preferredFramesPerSecond = 30
} else {
timer.frameInterval = 2
}
apply(interval: refreshRateInterval, to: timer)
timer.add(to: .main, forMode: .default)
timer.add(to: .main, forMode: .tracking)
self.timer = timer
Expand Down
8 changes: 8 additions & 0 deletions Example/EFCountingLabel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
29AF0091229EE2CE006A18B4 /* FrameRateViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29AF0090229EE2CE006A18B4 /* FrameRateViewController.swift */; };
5EEE4459477CD89613CC9254 /* Pods_EFCountingLabel_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EA8BF1F2807CCE416BEF5C0C /* Pods_EFCountingLabel_Tests.framework */; };
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACD51AFB9204008FA782 /* AppDelegate.swift */; };
607FACDB1AFB9204008FA782 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 607FACD91AFB9204008FA782 /* Main.storyboard */; };
Expand All @@ -15,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 */; };
Expand All @@ -33,6 +35,7 @@
/* Begin PBXFileReference section */
0D6321623D5D5D7DD361D3FA /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
276DE02E85C0E7D6ED37EDD4 /* Pods-EFCountingLabel_Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EFCountingLabel_Example.debug.xcconfig"; path = "Pods/Target Support Files/Pods-EFCountingLabel_Example/Pods-EFCountingLabel_Example.debug.xcconfig"; sourceTree = "<group>"; };
29AF0090229EE2CE006A18B4 /* FrameRateViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FrameRateViewController.swift; sourceTree = "<group>"; };
378DEADB22321D1400F0F63D /* README_CN.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = README_CN.md; path = ../README_CN.md; sourceTree = "<group>"; };
378DEADC22321D1400F0F63D /* CHANGELOG.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; name = CHANGELOG.md; path = ../CHANGELOG.md; sourceTree = "<group>"; };
47A0BF93F64203C763BE2E42 /* Pods-EFCountingLabel_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EFCountingLabel_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-EFCountingLabel_Tests/Pods-EFCountingLabel_Tests.release.xcconfig"; sourceTree = "<group>"; };
Expand All @@ -46,6 +49,7 @@
607FACEA1AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
6154772B1F9DBE58D87528C6 /* ListTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListTableViewController.swift; sourceTree = "<group>"; };
6154777F75D51F92326A369C /* DynamicFrameRateViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicFrameRateViewController.swift; sourceTree = "<group>"; };
615477C97BA7D1BE03393744 /* StoryboardLabelViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardLabelViewController.swift; sourceTree = "<group>"; };
615478D4657C10BDDB2930B2 /* CustomTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomTableViewController.swift; sourceTree = "<group>"; };
615478D55F97750BE62D6350 /* StoryboardButtonViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StoryboardButtonViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -156,6 +160,8 @@
615478D55F97750BE62D6350 /* StoryboardButtonViewController.swift */,
615477C97BA7D1BE03393744 /* StoryboardLabelViewController.swift */,
615478D4657C10BDDB2930B2 /* CustomTableViewController.swift */,
29AF0090229EE2CE006A18B4 /* FrameRateViewController.swift */,
6154777F75D51F92326A369C /* DynamicFrameRateViewController.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -345,9 +351,11 @@
files = (
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
61547AB8DD5A704F70185C87 /* ListTableViewController.swift in Sources */,
29AF0091229EE2CE006A18B4 /* FrameRateViewController.swift in Sources */,
615470077217336459F28548 /* StoryboardButtonViewController.swift in Sources */,
61547FDE291FDA37914D8D74 /* StoryboardLabelViewController.swift in Sources */,
61547605835C3408C93468DF /* CustomTableViewController.swift in Sources */,
6154767B05F9D92C4A3CCD3D /* DynamicFrameRateViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Loading