Skip to content

Instantly share code, notes, and snippets.

@mcousillas6
Created January 7, 2019 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcousillas6/b2f3041e9a6b902292f6f751d137067b to your computer and use it in GitHub Desktop.
Save mcousillas6/b2f3041e9a6b902292f6f751d137067b to your computer and use it in GitHub Desktop.
Custom Lottie Button on iOS
/**
let button = LottieButton(
with: .zero,
and: "success_animation",
onTap: { view in
print("Tapped")
}
)
*/
class LottieButton: LOTAnimationView {
var tapHandler: ((LottieButton) -> Void)?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(with frame: CGRect, and animationName: String, onTap: ((LottieButton) -> Void)? = nil) {
super.init(frame: frame)
tapHandler = onTap
setAnimation(named: animationName)
setupToucheHandling()
}
func setupToucheHandling() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(viewTapped(sender:)))
tapRecognizer.delegate = self
addGestureRecognizer(tapRecognizer)
}
@objc func viewTapped(sender: UIGestureRecognizer) {
tapHandler?(self)
}
}
extension LottieButton: UIGestureRecognizerDelegate {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
alpha = 0.3
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
alpha = 1
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
alpha = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment