LKS_GestureTargetActionsSearcher.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifdef SHOULD_COMPILE_LOOKIN_SERVER
  2. //
  3. // LKS_GestureTargetActionsSearcher.m
  4. // LookinServer
  5. //
  6. // Created by likai.123 on 2023/9/11.
  7. //
  8. #import "LKS_GestureTargetActionsSearcher.h"
  9. #import <objc/runtime.h>
  10. #import "NSArray+Lookin.h"
  11. #import "LookinTuple.h"
  12. #import "LookinWeakContainer.h"
  13. @implementation LKS_GestureTargetActionsSearcher
  14. + (NSArray<LookinTwoTuple *> *)getTargetActionsFromRecognizer:(UIGestureRecognizer *)recognizer {
  15. if (!recognizer) {
  16. return @[];
  17. }
  18. // KVC 要放到 try catch 里面防止 Crash
  19. @try {
  20. NSArray* targetsList = [recognizer valueForKey:@"_targets"];
  21. if (!targetsList || targetsList.count == 0) {
  22. return @[];
  23. }
  24. // 数组元素对象是 UIGestureRecognizerTarget*
  25. // 这个元素有两个属性,一个是名为 _target 的属性指向某个实例,另一个是名为 _action 的属性保存一个 SEL
  26. NSArray<LookinTwoTuple *>* ret = [targetsList lookin_map:^id(NSUInteger idx, id targetBox) {
  27. id targetObj = [targetBox valueForKey:@"_target"];
  28. if (!targetObj) {
  29. return nil;
  30. }
  31. SEL action = ((SEL (*)(id, Ivar))object_getIvar)(targetBox, class_getInstanceVariable([targetBox class], "_action"));
  32. LookinTwoTuple* tuple = [LookinTwoTuple new];
  33. tuple.first = [LookinWeakContainer containerWithObject:targetObj];
  34. tuple.second = (action == NULL ? @"NULL" : NSStringFromSelector(action));
  35. return tuple;
  36. }];
  37. return ret;
  38. }
  39. @catch (NSException * e) {
  40. NSLog(@"LookinServer - %@", e);
  41. return @[];
  42. }
  43. }
  44. @end
  45. #endif /* SHOULD_COMPILE_LOOKIN_SERVER */