Ok, I did the sample project specially for you. Here what I did:
- Subclassed UIView class and created CustomView.
- In the storyboard in the Identity inspector section set the view's Class as CustomView
On screenshot you may notice the view hierarchy, it repeats your concept.
Here is overridden hitTest:withEvent
in CustomView.m:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
for (UIView *subview in self.subviews.reverseObjectEnumerator) {
CGPoint subPoint = [subview convertPoint:point fromView:self];
UIView *result = [subview hitTest:subPoint withEvent:event];
if (result != nil && [result isKindOfClass:[UIButton class]]) {
return result;
}
}
}
return [super hitTest:point withEvent:event];
}
This method traverses the view hierarchy by calling the pointInside:withEvent:
method of each subview to determine which subview should receive a touch event. If pointInside:withEvent:
returns YES, then the subview’s hierarchy is similarly traversed until the frontmost view containing the specified point is found. If a view does not contain the point, its branch of the view hierarchy is ignored. You rarely need to call this method yourself, but you might override it to hide touch events from subviews.
This method ignores view objects that are hidden, that have disabled user interactions, or have an alpha level less than 0.01. This method does not take the view’s content into account when determining a hit. Thus, a view can still be returned even if the specified point is in a transparent portion of that view’s content.
On the dessert: Sample Project