Created
February 18, 2019 09:43
-
-
Save heestand-xyz/4f72fff09f5dcd9c2bb4896ef00718c9 to your computer and use it in GitHub Desktop.
Simple Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var filterCache: [CGFloat] = [] | |
func filter(_ value: CGFloat, for seconds: CGFloat) -> CGFloat { | |
filterCache.append(value) | |
guard filterCache.count > 1 else { return value } | |
let frameCount = Int(seconds * CGFloat(UIScreen.main.maximumFramesPerSecond)) | |
if filterCache.count > frameCount { | |
filterCache.remove(at: 0) | |
} | |
var filtered: CGFloat = 0.0 | |
for val in filterCache { | |
filtered += val | |
} | |
filtered /= CGFloat(filterCache.count) | |
return filtered | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment