Search code examples
objective-cuiviewuiimageviewcgpathuser-interaction

Allow user interaction outside a non rectangular UIView content


On a UIView that shows a circle image (needs to stay interactive), how would you prevent the area out side of the circle from receiving any user interactions, so the other ui under that view will still be active?

i tried masking the UIView with CGPath but that didn't help.enter image description here

any ideas?


Solution

  • Since a touch event bubble down in the view hierarchy, as a UIView, you can check if a touch event is relevant to you, if it is not just return NO and that event will travel down to the next UIView in the hierarchy

    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        for (UIView * view in [self subviews]) {
            if ([view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
                return YES;
            }
        }
        return NO;
    }