我有 3 个 UIView,分层在一个大 uiview 之上。我想知道用户是否触摸了最上面的一个而不关心其他的。我将在第二个 UIView 中有几个按钮,在第三个 UIView 中有一个 UITable。
问题是我在第一个视图上打开 userInteractionEngabled 并且有效,但即使我将其关闭,所有其他视图也会以相同的方式响应。如果我在 self.view 上禁用 userInteractionEnabled ,它们都不会响应。我也无法检测到 TouchBegan 委托方法中触摸了哪个视图。
My code:
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aView = userInteractionEnabled = YES;
[self.view addSubview:aView];
UIView *bView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 50)];
bView.userInteractionEnabled = NO;
[self.view addSubview:bView];
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//This gets called for a touch anywhere
}
为了检查另一个视图中的某个视图是否被触摸,您可以使用 hitTest。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event;
在touchesBegan的自定义实现中,检查touches集中的每个触摸。 hitTest 方法的点可以使用以下方法获得
- (CGPoint)locationInView:(UIView *)view;
方法,其中视图是您的超级视图(包含其他视图的视图)。
编辑:这是一个快速的自定义实现:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint locationPoint = [[touches anyObject] locationInView:self];
UIView* viewYouWishToObtain = [self hitTest:locationPoint withEvent:event];
}
我希望这可以帮到你,
保罗
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)