Skip to content

Instantly share code, notes, and snippets.

@heestand-xyz
Last active October 17, 2018 08:45
Show Gist options
  • Save heestand-xyz/25c94a5470bb71dea1399013815408ab to your computer and use it in GitHub Desktop.
Save heestand-xyz/25c94a5470bb71dea1399013815408ab to your computer and use it in GitHub Desktop.
Animate
func animate(for duration: CGFloat, loop: @escaping (CGFloat) -> (), done: @escaping () -> ()) {
let startTime = Date()
RunLoop.current.add(Timer(timeInterval: 1.0 / Double(UIScreen.main.maximumFramesPerSecond), repeats: true, block: { t in
let elapsedTime = CGFloat(-startTime.timeIntervalSinceNow)
let fraction = min(elapsedTime / duration, 1.0)
loop(fraction)
if fraction == 1.0 {
done()
t.invalidate()
}
}), forMode: .common)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment