Skip to content

Instantly share code, notes, and snippets.

@heestand-xyz
Created February 18, 2019 09:43
Show Gist options
  • Save heestand-xyz/4f72fff09f5dcd9c2bb4896ef00718c9 to your computer and use it in GitHub Desktop.
Save heestand-xyz/4f72fff09f5dcd9c2bb4896ef00718c9 to your computer and use it in GitHub Desktop.
Simple Filter
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