Skip to content

Instantly share code, notes, and snippets.

@Alexander-Ignition
Created July 28, 2018 13:41
Show Gist options
  • Save Alexander-Ignition/ce66bf02c55b1490381bd78b8a11918e to your computer and use it in GitHub Desktop.
Save Alexander-Ignition/ce66bf02c55b1490381bd78b8a11918e to your computer and use it in GitHub Desktop.
Smart key path using with CABasicAnimation
class KeyPathAnimation<View, Value> {
let basicAnimation: CABasicAnimation
let keyPath: KeyPath<View, Value>
init(keyPath: KeyPath<View, Value>) {
self.keyPath = keyPath
// https://github.com/apple/swift/blob/2844583d7f9d51ce5c4da7776e19f0aab6f2e3f6/stdlib/public/core/KeyPath.swift#L107
basicAnimation = CABasicAnimation(keyPath: keyPath._kvcKeyPathString)
}
// MARK: - CABasicAnimation
var fromValue: Value? {
get {
return basicAnimation.fromValue as? Value
}
set {
basicAnimation.fromValue = newValue
}
}
var toValue: Value? {
get {
return basicAnimation.toValue as? Value
}
set {
basicAnimation.toValue = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment