Created
December 7, 2018 18:05
-
-
Save vialyx/65c0c8126f44f87fe31c359cf5655df3 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
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