Ios 当userinteractionenabled为NO时获得联系

Ios 当userinteractionenabled为NO时获得联系,ios,uigesturerecognizer,Ios,Uigesturerecognizer,似乎有一个错误,但当我尝试提供的方法时,它就是不起作用 @interface MyView : UIView @end @implementation MyView - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; NSLog(@"hitView:%@", hitView);

似乎有一个错误,但当我尝试提供的方法时,它就是不起作用

@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];
}
myView.userInteractionEnabled=YES时一切正常。但是当
myView.userInteractionEnabled=NO时只要我点击屏幕,它就会输出
hitView:(null)


那么,这个方法不再有效了,还是我错过了什么?

是的,因为您禁用了用户交互。
调用超级hittest时,超级hittest的返回取决于userInteraction属性。

如果仅启用,则返回自身或某个子视图。

如果未启用,它将始终返回nil。

如果我理解正确,您希望知道在自定义UI视图中何时发生触摸,而视图上的用户交互被禁用。如果是这样,请在-(UIView*)hitTest:(CGPoint)PointWithEvent:(UIEvent*)事件方法中抛出以下内容。此条件应为自定义视图中的只读触摸事件

if ([self pointInside:point withEvent:event]) {
    NSLog(@"TOUCH IS INSIDE THE VIEW");
}