Skip to content

Instantly share code, notes, and snippets.

@edudnyk
Last active July 30, 2019 21:38
Show Gist options
  • Save edudnyk/1204277e76531c85a85e4c4d7d26b2b4 to your computer and use it in GitHub Desktop.
Save edudnyk/1204277e76531c85a85e4c4d7d26b2b4 to your computer and use it in GitHub Desktop.
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