如何在UIView中剪出一个洞,而不是隐藏子主题?



我使用下面的代码在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

我有什么选择

。遮蔽父视图遮蔽了它的子视图。这就是掩码的含义

然而,你可以用其他方法来制造这个洞。例如,只需屏蔽视图绘制其内容的方式,而不是屏蔽视图本身。

或者,也许更容易,使用位于同一位置的两个视图:一个是可见的,具有孔掩模,另一个是有效不可见的,因为它的背景几乎(但不是完全)透明,并具有子视图。

最新更新