Created
November 23, 2018 04:35
-
-
Save HarshilShah/97573cc2962cd15cd2ab2dc1f70f6104 to your computer and use it in GitHub Desktop.
A UIViewPropertyAnimator subclass that adds a preparation phase
This file contains 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
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