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

时间:2017-06-10 09:36:57

标签: ios swift swift3 uicolor cgpath

我正在使用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

1 个答案:

答案 0 :(得分:3)

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

    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)

如果有任何错误,请尝试并发表评论。快乐的编码。