如何在 cgmutablepath 中设置形状的颜色?

How to set the color for shape in cgmutablepath?

我正在使用 cgmutablepath 绘制一个多边形,我想在其中为多边形填充颜色,但它不起作用,我不知道为什么...

下面是我的代码:

var path = CGMutablePath()
        path.move(to: CGPoint(x: 127.5, y: 12.5))
        path.addLine(to: CGPoint(x: 165.0, y: 13.0))
        path.addLine(to: CGPoint(x: 165.0, y: 50.5))
        path.addLine(to: CGPoint(x: 127.5, y: 50.5))
        path.closeSubpath()

       var shape = CAShapeLayer()
       shape.path = path
       shape.lineWidth = 3.0
       shape.strokeColor = UIColor.blue.cgColor
       shape.fillColor = UIColor.blue.cgColor

尝试用更新后的代码替换您的代码:

    var path = CGMutablePath()
    path.move(to: CGPoint(x: 127.5, y: 12.5))
    path.addLine(to: CGPoint(x: 165.0, y: 13.0))
    path.addLine(to: CGPoint(x: 165.0, y: 50.5))
    path.addLine(to: CGPoint(x: 127.5, y: 50.5))
    path.closeSubpath()

   var shape = CAShapeLayer()
   shape.path = path
   shape.lineWidth = 3.0
   shape.strokeColor = UIColor.blue.cgColor
   shape.fillColor = UIColor.blue.cgColor
   self.layer.addSublayer(shapeLayer)

请试用,如有错误请留言。快乐编码。