簡體   English   中英

如何在iOS中與CAShapeLayers相交

[英]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圖像,它將很好用,並且您無需繪制加號。

您可以通過以下方法實現這一目標:

  1. 創建圓CAShapeLayercircleShape );
  2. 創建具有與circleShape相同顏色的反轉蒙版層( inverted )。 對於這種情況,您需要一個CGMutablePath具有完全相同的圓路徑和加路徑。 另外,不要忘記設置inverted.fillRule = .evenOdd
  3. 比您只需要一個帶有透明加號的圖層( plusShape );
  4. 添加circleShape作為子圖層以查看圖層;
  5. 設置掩碼: circleShape.mask = inverted
  6. plusShape添加為子圖層以查看視圖的圖層。

而已! 現在您有了透明加唱歌。 例:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM