如何在 UIView 中切出一个洞而不隐藏子视图?
How to cut out a hole in UIView and not hide subiews?
我正在使用下面的代码在 UIView 中创建一个洞,但我需要找到一种方法来不剪切整个父视图的子视图。我有哪些选择?
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
What options do I have
None。屏蔽父视图会屏蔽它的子视图。这就是面具 是 .
但是,您可以用其他方式打洞。例如,只屏蔽视图绘制其内容的方式而不是屏蔽视图本身。
或者,也许更容易,使用位于同一位置的两个视图:一个是可见的并且有孔掩码,另一个实际上是不可见的,因为它的背景几乎(但不是完全)透明并且有子视图。
我正在使用下面的代码在 UIView 中创建一个洞,但我需要找到一种方法来不剪切整个父视图的子视图。我有哪些选择?
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
What options do I have
None。屏蔽父视图会屏蔽它的子视图。这就是面具 是 .
但是,您可以用其他方式打洞。例如,只屏蔽视图绘制其内容的方式而不是屏蔽视图本身。
或者,也许更容易,使用位于同一位置的两个视图:一个是可见的并且有孔掩码,另一个实际上是不可见的,因为它的背景几乎(但不是完全)透明并且有子视图。