Last active
October 17, 2018 08:45
-
-
Save heestand-xyz/25c94a5470bb71dea1399013815408ab to your computer and use it in GitHub Desktop.
Animate
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
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