Skip to content

Instantly share code, notes, and snippets.

@natebirkholz
Created August 9, 2021 16:24
Show Gist options
  • Save natebirkholz/ce01a5abd07a44f5a694929cdc03c8e8 to your computer and use it in GitHub Desktop.
Save natebirkholz/ce01a5abd07a44f5a694929cdc03c8e8 to your computer and use it in GitHub Desktop.
class ClosureGestureRecognizer<GestureRecognizer: UIGestureRecognizer> {
fileprivate var recognizer: GestureRecognizer
private var actionHandler: ((GestureRecognizer) -> Void)
init(action: @escaping (GestureRecognizer) -> ()) {
self.recognizer = GestureRecognizer()
self.actionHandler = action
self.recognizer.addTarget(self, action: #selector(handleAction))
}
@objc private func handleAction() {
actionHandler(recognizer)
}
}
extension UIView {
func addGestureRecognizer(_ gestureRecognizer: ClosureGestureRecognizer) {
self.addGestureRecognizer(gestureRecognizer.recognizer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment