UIGestureRecognizer+Blocks.m 813 B

12345678910111213141516171819202122232425262728293031323334353637
  1. //
  2. // UIGestureRecognizer+Blocks.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 12/20/19.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "UIGestureRecognizer+Blocks.h"
  9. #import <objc/runtime.h>
  10. @implementation UIGestureRecognizer (Blocks)
  11. static void * actionKey;
  12. + (instancetype)flex_action:(GestureBlock)action {
  13. UIGestureRecognizer *gesture = [[self alloc] initWithTarget:nil action:nil];
  14. [gesture addTarget:gesture action:@selector(flex_invoke)];
  15. gesture.flex_action = action;
  16. return gesture;
  17. }
  18. - (void)flex_invoke {
  19. self.flex_action(self);
  20. }
  21. - (GestureBlock)flex_action {
  22. return objc_getAssociatedObject(self, &actionKey);
  23. }
  24. - (void)flex_setAction:(GestureBlock)action {
  25. objc_setAssociatedObject(self, &actionKey, action, OBJC_ASSOCIATION_COPY);
  26. }
  27. @end