我正在尝试查找UIGestureRecognizer在哪个目标上触发了哪个操作。很遗憾,UIGestureRecognizer
上没有gesture.action
或gesture.target
的属性。我正在分析的手势是UIKit私有实施的一部分。
此处部分答案
答案 0 :(得分:5)
这是一个代码片段,列出与手势识别器关联的所有目标/操作对:
Ivar targetsIvar = class_getInstanceVariable([UIGestureRecognizer class], "_targets");
id targetActionPairs = object_getIvar(gesture, targetsIvar);
Class targetActionPairClass = NSClassFromString(@"UIGestureRecognizerTarget");
Ivar targetIvar = class_getInstanceVariable(targetActionPairClass, "_target");
Ivar actionIvar = class_getInstanceVariable(targetActionPairClass, "_action");
for (id targetActionPair in targetActionPairs)
{
id target = object_getIvar(targetActionPair, targetIvar);
SEL action = (__bridge void *)object_getIvar(targetActionPair, actionIvar);
NSLog(@"target=%@; action=%@", target, NSStringFromSelector(action));
}
请注意,您必须导入<objc/runtime.h>
,并且这会使用私有的ivars和一个类,因此它可能会让您从App Store中被禁止。
答案 1 :(得分:0)
我有一个不同的解决方案,这对我有用。这更像是一次设计更改......您无法从捕获的手势中访问目标。因此,在触碰发生时和平底锅开始之前,请保持对物体的引用。
@property (nonatomic, strong) UIButton *myTouchedButton; // reference to button
(void)init
{
...
[card.button addTarget:self action:@selector(cardTouchDownInside:) forControlEvents:UIControlEventTouchDown];
...
}
-(void)cardTouchDownInside:(id)sender
{
NSLog(@"touch down on object");
self.myTouchedButton = (UIButton*)sender;
}