[英]How to intersect CAShapeLayers in iOS
我有一個加號(+),目前正以藍色顯示,但是我想使其透明,以便用戶可以看到背景。 加層添加到更大的視圖。 設置加層以clear
顏色不能解決問題。
class AddButtonView: UIView {
...
private func setupPlusLayer() {
let path = UIBezierPath()
path.move(to: CGPoint(x: plusButton.frame.midX, y: plusButton.frame.midY-20))
path.addLine(to: CGPoint(x: plusButton.frame.midX, y: plusButton.frame.midY+20))
path.move(to: CGPoint(x: plusButton.frame.midX-20, y: plusButton.frame.midY))
path.addLine(to: CGPoint(x: plusButton.frame.midX+20, y: plusButton.frame.midY))
path.usesEvenOddFillRule = true
let shapeLayer = CAShapeLayer()
shapeLayer.fillRule = .evenOdd
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.lineWidth = 4
// Add that `CAShapeLayer` to your view's layer:
self.layer.addSublayer(shapeLayer)
}
}
如何使加號透明?
嘗試使用帶+透明的.png圖像,它將很好用,並且您無需繪制加號。
您可以通過以下方法實現這一目標:
CAShapeLayer
( circleShape
); circleShape
相同顏色的反轉蒙版層( inverted
)。 對於這種情況,您需要一個CGMutablePath
具有完全相同的圓路徑和加路徑。 另外,不要忘記設置inverted.fillRule = .evenOdd
; plusShape
); circleShape
作為子圖層以查看圖層; circleShape.mask = inverted
; plusShape
添加為子圖層以查看視圖的圖層。 而已! 現在您有了透明加唱歌。 例:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.