UIView subview touch forward

2019-06-08 05:16发布

问题:

I have two custom UIViews (A and B respectively) each with a frame set to the same origin and same size. They are both added to a parent UIView (C) as subviews.

I have a touch recognizer as part of A and B that listen for touches on certain spots. If that touch is received, it raises a delegate up to the parent UIView saying it has been touched. The issue is that since B is added after A, B never receives a touch event.

The A and B UIViews are single lines with endpoints. They both recognize touches on the endpoints and raise up delegate notifications. How do I add subviews and recognize touches on them with parent views on top? It's like an exclusivity thing.

回答1:

If A is receiving touch events and B isn't though they're both children of C, then it's because A is claiming all touch events for itself, including those intended for B.

The solution is to override the pointInside:withEvent: method on A in order to determine whether it should accept the event for itself, like this:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointIsInHotspot:point]) return YES;
    return NO;
}

Where pointIsInHotspot is whatever tests you need to perform in order to determine if the point is on an active area within the view.



回答2:

Set cancelsTouchesInView to NO in your gesture recognizer.



标签: ios uiview