查找UIGestureRecognizer操作(选择器)名称和目标

时间:2013-11-19 08:12:05

标签: ios iphone ipad uikit

我正在尝试查找UIGestureRecognizer在哪个目标上触发了哪个操作。很遗憾,UIGestureRecognizer上没有gesture.actiongesture.target的属性。我正在分析的手势是UIKit私有实施的一部分。

此处部分答案

stackOverFlow Question 20066315

2 个答案:

答案 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;
}