I have 3 UIViews, layered on top of one large uiview. I want to know if the user touches the top one and not care about the other ones. I will have a couple of buttons in the second UIView and a UITable in the 3rd UIView.
Problem is I turn userInteractionEngabled on on the first view and that works, but all the other views respond in the same way even if I turn it off. If I disable userInteractionEnabled on self.view none of them respond. I also can't detect which view was touched in the touchesBegan delegate method.
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
}
In touchesBegan check that the touched view is the one you want, solved the problem for me when I tried to identified if the user touched a specific ImageView.
If you adjust the content for the view diamensions make sure to change the view size accordingly otherwise it will recognize touch event even in area where there in so content in the view. In my case when I tried to keep the image proportion with UIViewContentModeScaleAspectFit although the image was adjusted as required in the "white spaces" areas touch event were catched as well.