Skip to content

Instantly share code, notes, and snippets.

@HarshilShah
Created November 23, 2018 04:35
Show Gist options
  • Save HarshilShah/97573cc2962cd15cd2ab2dc1f70f6104 to your computer and use it in GitHub Desktop.
Save HarshilShah/97573cc2962cd15cd2ab2dc1f70f6104 to your computer and use it in GitHub Desktop.
A UIViewPropertyAnimator subclass that adds a preparation phase
import UIKit
final class HSViewPropertyAnimator: UIViewPropertyAnimator {
private var preparations: [(UIViewAnimatingPosition) -> ()] = []
override func startAnimation() {
runPreparations()
super.startAnimation()
}
func addPreparation(_ preparation: @escaping (UIViewAnimatingPosition) -> ()) {
preparations.append(preparation)
}
private func runPreparations() {
let position: UIViewAnimatingPosition = {
switch self.fractionComplete {
case 0: return .start
case 1: return .end
default: return .current
}
}()
preparations.forEach { $0(position) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment