I have some nested views like this:
First, there is an big UIView. Inside this, there is another UIView. And inside this, there is an MyNiceButtons class that inherits from UIView. So I have:
UIView > UIView > MyNiceButtons (= an UIView).
In detail, the UIView creates an UIImageView and adds it as a child view to itself, which represents an graphical button. So inside the MyNiceButtons class implementation, I have this code to handle a touch event on the fake button image:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject]; // i just need single touches
// just to see which view is affected
UIView *tview = [touch view];
CGRect rect = tview.frame;
rect.origin.x = rect.origin.x + 20.0f;
tview.frame = rect;
[tview setBackgroundColor:[UIColor yellowColor]];
if ([touch view] == self.fakeButtonImageView) {
NSLog(@"touch on fake button detected");
}
}
All views in the hierarchy have userInteractionEnabled=YES. The touch arrives at the first view in the hierarchy (the parent of the parent).
UIView *tview = [touch view];
This returns just the parent of the parent, but not the UIImageView where the user actually tapped on.
What's the trick to recognize if the touch was on the button UIImageView?