0

我有一个 UIScrollView,里面有一些视图。当我点击它时,我希望能够了解触摸是发生在干净区域还是存在子视图的区域。我怎样才能做到这一点?

4

1 回答 1

0

我想我在Find which child view was taped 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]));
}
于 2012-12-04T17:51:52.777 回答