Jose Solorzano
Jose Solorzano

Reputation: 3

How to cut out a hole in UIView and not hide subiews?

I'm creating a hole in a UIView using the code below but I need to find a way to not cutout subviews of that superview with the whole. What options do I have?

let hole = self.convert(bubble.frame, from: bubble)
            let path = UIBezierPath(rect: view.bounds)
            let pathWithRadius = UIBezierPath(roundedRect: hole, byRoundingCorners: [.allCorners], cornerRadii: CGSize(width: 17.0, height: 17.0))
            path.append(pathWithRadius)

            // Create a shape layer and cut out the intersection
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            mask.fillRule = CAShapeLayerFillRule.evenOdd
            
            // Add the mask to the view
            view.layer.mask = mask

Upvotes: 0

Views: 167

Answers (1)

matt
matt

Reputation: 535860

What options do I have

None. Masking a superview masks out its subviews. That's what a mask is.

However, you could make the hole in some other way. For example, just mask the way the view draws its contents rather than masking the view itself.

Alternatively, and perhaps easier, use two views located in the same spot: one that is visible and has the hole mask, the other that is effectively invisible because its background is nearly (but not quite) transparent and has the subviews.

Upvotes: 2

Related Questions