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

标签 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

最佳答案

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

    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)

请尝试一下,如有错误请评论。快乐编码。

关于ios - 如何在 cgmutablepath 中设置形状的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44471799/

相关文章:

ios - Struct Gobal 在不应该的时候从另一个 VC 更新

ios - SQLite - 外键约束 - IOs 5

ios - 带有白色边框的 alertController

ios - 将 X 轴标签分配给图表框架

swift - 如何提取 zip 文件并在 Swift 的共享扩展中获取提取的组件

ios - 委托(delegate)必须响应 locationManager :didFailWithError: even though implemented didFailWithError method

ios - swift中CBUUID和NSUUID的区别

ios - 在 Swift 中显示表情符号

ios - 在 iOS 上使用 Alamofire 的 Twilio POST 请求

ios - 限制 NSURLConnection 数据速率? (带宽限制)