1

There seems to be an answer, but when I try the method provided, it just doesn't work!

@interface MyView : UIView

@end

@implementation MyView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    NSLog(@"hitView:%@", hitView);
    if (hitView == self) {
        return nil;
    }
    return hitView;
}

- (void)testUserInteraction
{
    UIViewController *vc = [[UIViewController alloc] init];
    self.window.rootViewController = vc;

    MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    myView.userInteractionEnabled = NO;
    [vc.view addSubview:myView];

    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    subView.backgroundColor = [UIColor orangeColor];
    [myView addSubview:subView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped)];
    [subView addGestureRecognizer:tap];
}

When myView.userInteractionEnabled = YES; everything works fine. but when myView.userInteractionEnabled = NO; wherever I tapped the screen, it just output hitView:(null)

So is this method no longer working, or did I miss something?

Community
  • 1
  • 1
limboy
  • 3,879
  • 7
  • 37
  • 53

2 Answers2

1

yes, because you disabled user interaction.
When you call super-hittest,the return from super-hittest depends on userInteraction property.

If it is enabled then only, it returns either itself or some subView.
If not enabled, it will always return nil.

santhu
  • 4,796
  • 1
  • 21
  • 29
0

If I understand correctly, you want to know when a touch happens inside your custom UIView, while user interaction on the view is disabled. If this is so, throw the following inside the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method. This condition should read only touch events inside your custom view

if ([self pointInside:point withEvent:event]) {
    NSLog(@"TOUCH IS INSIDE THE VIEW");
}
Daniel McCarthy
  • 1,406
  • 2
  • 13
  • 19