似乎有一个answer ,但是当我尝试提供的方法时,它就是行不通!
@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)
那么是这个方法不再有效了,还是我遗漏了什么?
最佳答案
是的,因为您禁用了用户交互。
当您调用 super-hittest 时,super-hittest 的返回值取决于 userInteraction 属性。
如果它被启用,那么它只会返回它自己或一些 subview 。
如果未启用,它将始终返回 nil。
关于ios - 当 userinteractionenabled 为 NO 时获取触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21248901/