问题描述
我正在尝试查找UIGestureRecognizer在哪个目标上触发了哪个操作。很遗憾, UIGestureRecognizer
上没有属性,例如 gesture.action
或 gesture.target
。我正在分析的手势是UIKit私人实施的一部分。
I'm trying to find which action is triggered by a UIGestureRecognizer on which target. Unfortunately there is no property on a UIGestureRecognizer
such as gesture.action
or gesture.target
. The gesture I'm analyzing is part of UIKit private implementation.
这里的部分答案
推荐答案
这是一个代码片段,列出与手势识别器关联的所有目标/动作对:
Here's a code snippet that will list all target/action pairs associated with a gesture recognizer:
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中被禁止。
Note that you'll have to import <objc/runtime.h>
, and that this uses private ivars and a class, so it could get you banned from the App Store.
这篇关于查找UIGestureRecognizer操作(选择器)名称和目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!