UITouch事件转发到的所有子视图的列表

时间:2012-12-04 16:44:52

标签: subview uitouch uitapgesturerecognizer first-responder

我有一个内部有一些视图的UIScrollView。当我点击它时,我希望能够理解触摸是发生在干净区域还是有子视图的区域。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我想我在Find which child view was tapped when using UITapGestureRecognizer

中找到了一个可能的解决方案

所以,在使用以下方式注册 UITapGestureRecognizer

//        // Intercept all the taps inside the view
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(tapDetected:)];
        tapRecognizer.numberOfTapsRequired = 1;
        tapRecognizer.numberOfTouchesRequired = 1;
        tapRecognizer.cancelsTouchesInView = NO;
        [self addGestureRecognizer:tapRecognizer];

这足以包含此代码:

- (void)tapDetected:(UITapGestureRecognizer*)recognizer
{
    UIView* view = recognizer.view;
    CGPoint loc = [recognizer locationInView:view];
    UIView* subview = [view hitTest:loc withEvent:nil];

    NSLog(@"HIT! in %@",NSStringFromClass([subview class]));
}