简体   繁体   English

如何知道正在接收当前手势的视图

[英]how to know the view that is receiving the current gesture

I have a UIButton that is only reacting to my tap gestures when I tap on a very small sub-area within it. 我有一个UIButton ,当我在其中的一个很小的子区域上点击时,它只会对我的点击手势做出反应。 I'm assuming that maybe another view is swallowing my gestures when I tap in other places within the button. 假设当我在按钮内的其他位置点击时,其他视图可能正在吞噬我的手势。

the question is.. is there a way to know which UIView exactly is receiving the taps? 问题是..是否有办法知道哪个UIView确切接收水龙头? I know I can brute force it an attach all gesture handlers to all of my views.. but seems like too much work. 我知道我可以蛮力将所有手势处理程序附加到我的所有视图中..但似乎工作太多。 any ideas? 有任何想法吗?

update: Ahmed's answer below worked perfectly.. I also added to it UIView's recursiveDescription to find the offending view quickly. 更新:下面的艾哈迈德的答案完美地工作了。我还添加了UIView的recursiveDescription来快速找到有问题的视图。

ie if i put a breakpoint on the NSLog line: 即如果我在NSLog行上放置一个断点:

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    UIView *touchReceipientView =((UITouch *)[event.allTouches anyObject]).view;
    NSLog(@"View = %@ ",touchReceipientView);
}

I get this output (as an example): 我得到以下输出(作为示例):

(lldb) po [touchReceipientView recursiveDescription]
<UIView: 0x1fd16470; frame = (0 430; 320 40); layer = <CALayer: 0x1fd164d0>>
   | <UIButton: 0x1fd13030; frame = (297 0; 18 19); opaque = NO; tag = 11; layer = <CALayer: 0x1fd130f0>>
   |    | <UIImageView: 0x1fd13190; frame = (-41 -40.5; 100 100); alpha = 0; opaque = NO; userInteractionEnabled = NO; tag = 1886548836; layer = <CALayer: 0x1fd131f0>>
   |    | <UIImageView: 0x1fd14d00; frame = (1 2; 16 15); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1fdc8e50>>

which made me identify the offending UIView immediately! 这让我立即确定了有问题的UIView!

You can subclass the UIApplication and see who is receiving the events. 您可以将UIApplication子类化,并查看谁在接收事件。

and in main.m, 在main.m中,

@interface MyApplication : UIApplication

@end


@implementation MyApplication

- (void)sendEvent:(UIEvent *)event
{
    [super sendEvent:event];
    NSLog(@"View = %@",  ((UITouch *)[event.allTouches anyObject]).view);
}

@end





int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, NSStringFromClass([MyApplication class]), NSStringFromClass([MyAppDelegate class]));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM