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

时间:2013-07-17 08:41:30

标签: iphone ios objective-c uiview uikit

当我点击其中的一个非常小的子区域时,我有UIButton只对我的轻击手势作出反应。我assuming当我点击按钮内的其他位置时,可能是另一个视图正在吞咽我的手势。

问题是..有没有办法知道哪个UIView正好接收水龙头?我知道我可以蛮力强迫它将所有的手势处理程序附加到我的所有观点中......但似乎工作太多了。任何想法?

更新 艾哈迈德的回答完美无缺。我还加入了UIView的recursiveDescription来快速找到有问题的观点。

即如果我在NSLog线上放置一个断点:

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

我得到这个输出(作为例子):

(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>>

让我立刻识别出违规的UIView!

1 个答案:

答案 0 :(得分:2)

您可以继承UIApplication并查看谁正在接收事件。

并在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]));
    }
}