Skip to content

Instantly share code, notes, and snippets.

@calvingit
Created November 4, 2023 11:43
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 calvingit/a63c2e0c3971dadbe23eaca106d46e9f to your computer and use it in GitHub Desktop.
Save calvingit/a63c2e0c3971dadbe23eaca106d46e9f to your computer and use it in GitHub Desktop.
extension UIGestureRecognizer {
private struct AssociatedKeys {
static var action = "action"
}
private typealias GestureAction = ((UIGestureRecognizer) -> Void)
private var action: GestureAction? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.action) as? GestureAction
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.action, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
}
}
@objc private func handleGesture(sender: UIGestureRecognizer) {
self.action?(sender)
}
func addTargetBlock(_ block: @escaping (UIGestureRecognizer) -> Void) {
self.action = block
self.addTarget(self, action: #selector(handleGesture(sender:)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment