Created
November 4, 2023 11:43
-
-
Save calvingit/a63c2e0c3971dadbe23eaca106d46e9f to your computer and use it in GitHub Desktop.
This file contains 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
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