我有3个UIView,它们层叠在一个大的uiview上面。我想知道用户是否触摸了顶部的视图,并不关心其他视图。我会在第二个UIView中放置一些按钮,在第三个UIView中放置一个UITable。
问题是,我在第一个视图上打开userInteractionEngabled,并且它起作用,但所有其他视图都会以相同的方式响应,即使我关闭了它。如果我在self.view上禁用userInteractionEnabled,则它们都无法响应。我也无法检测到touchesBegan委托方法中触摸了哪个视图。
我的代码:
问题是,我在第一个视图上打开userInteractionEngabled,并且它起作用,但所有其他视图都会以相同的方式响应,即使我关闭了它。如果我在self.view上禁用userInteractionEnabled,则它们都无法响应。我也无法检测到touchesBegan委托方法中触摸了哪个视图。
我的代码:
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
}
nil
参数调用locationInView:
方法,则会返回在主窗口坐标系中的CGPoint
位置。 - Dafydd Williams