Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created December 7, 2018 18:05
Show Gist options
  • Save vialyx/65c0c8126f44f87fe31c359cf5655df3 to your computer and use it in GitHub Desktop.
Save vialyx/65c0c8126f44f87fe31c359cf5655df3 to your computer and use it in GitHub Desktop.
private struct AssociatedKeys {
static var highlightAnimation = "VIV_highlightAnimation"
}
extension HighlightAnimatable where Self: UIView {
private var animationAvailable: Bool {
get { return (objc_getAssociatedObject(self, &AssociatedKeys.highlightAnimation) as? Bool) ?? true }
set { objc_setAssociatedObject(self, &AssociatedKeys.highlightAnimation, newValue, .OBJC_ASSOCIATION_ASSIGN) }
}
var settings: HighlightAnimatableSettings { return HighlightAnimatableSettings() }
func lockAnimation() {
animationAvailable = false
layer.removeAllAnimations()
}
func unlockAnimation() {
animationAvailable = true
}
func highlight(_ touched: Bool) {
highlight(touched, completion: nil)
}
func highlight(_ touched: Bool, completion: ((Bool) -> Void)?) {
guard animationAvailable else { return }
UIView.animate(withDuration: settings.duration,
delay: settings.delay,
usingSpringWithDamping: settings.springDamping,
initialSpringVelocity: settings.springVelocity,
options: settings.options,
animations: {
self.transform = touched ? self.settings.transform : .identity
}, completion: completion)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment