Last active
July 30, 2019 21:38
-
-
Save edudnyk/1204277e76531c85a85e4c4d7d26b2b4 to your computer and use it in GitHub Desktop.
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
class LKBoundsDidChangeAnimation : CABasicAnimation { | |
@objc var bounds : CGRect = CGRect.zero | |
override func copy(with zone: NSZone? = nil) -> Any { | |
let result = super.copy(with: zone) | |
if let action = result as? Self { | |
action.bounds = bounds | |
return action | |
} | |
return result | |
} | |
} | |
class LKBoundsDidChangeAction : CAAction { | |
var pendingAnimation : LKBoundsDidChangeAnimation | |
init(fromBounds: CGRect) { | |
let rootContextKey = keyPath(\LKLabelLayer.currentBoundsDidChangeAnimation) | |
let animation = LKBoundsDidChangeAnimation(keyPath: | |
keyPath(rootContextKey, suffix: keyPath(\LKBoundsDidChangeAnimation.bounds))) | |
animation.fromValue = fromBounds | |
animation.duration = UIView.inheritedAnimationDuration > 0 ? UIView.inheritedAnimationDuration : | |
CATransaction.animationDuration() | |
animation.bounds = fromBounds | |
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) | |
pendingAnimation = animation | |
} | |
func run(forKey event: String, object anObject: Any, arguments dict: [AnyHashable : Any]?) { | |
guard let textLayer = anObject as? LKLabelLayer, event == keyPath(\LKLabelLayer.bounds) else { return } | |
if !pendingAnimation.bounds.equalTo(textLayer.bounds) { | |
pendingAnimation.toValue = textLayer.bounds | |
textLayer.add(pendingAnimation, forKey:keyPath(\LKBoundsDidChangeAnimation.bounds)) | |
} | |
} | |
} | |
class LKCompositeAction : CAAction { | |
var actions : [CAAction] | |
init(actions: [CAAction]) { | |
self.actions = actions | |
} | |
func run(forKey event: String, object anObject: Any, arguments dict: [AnyHashable : Any]?) { | |
actions.forEach { (action) in | |
action.run(forKey: event, object: anObject, arguments: dict) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment