UIBezierPath无法更改该行的颜色

时间:2015-11-18 00:03:21

标签: ios swift uicolor uibezierpath

这是我的代码

 func  drawSmallCircles(){
        for oneArray in points {
            let startAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[0]))
            let endAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[1]))
            let radius: CGFloat = 100.0
            let path = UIBezierPath(arcCenter: center,
                radius: radius,
                startAngle: startAngleRadiant,
                endAngle: endAngleRadiant,
                clockwise: true)
            let color = UIColor(red: CGFloat(55), green: CGFloat(37), blue: CGFloat(23), alpha: CGFloat(1))
            path.fill()

            color.setFill()


            path.lineWidth = CGFloat(10)
            path.stroke()
            setNeedsDisplay()
        }
    }

如你所见,我有自定义颜色。我的问题是颜色总是黑色。

enter image description here

 override func drawRect(rect: CGRect) {
                self.opaque = false;
              drawSmallCircles()


        }

这是我的drawRec

UPDATE1

罗布回答后,这是我的代码

  let points = [[270, 290],[300, 320],[330, 350],[0, 20],[30, 50],[60, 80],[90, 110],[120, 140],[150, 170],[180, 200],[210, 230],[240, 260]]
 func  drawSmallCircles(){
        for oneArray in points {
            let startAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[0]))
            let endAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[1]))
            let radius: CGFloat = 50.0
            let path = UIBezierPath(arcCenter: CGPoint(x: CGFloat(100), y: CGFloat(100)),
                radius: radius,
                startAngle: startAngleRadiant,
                endAngle: endAngleRadiant,
                clockwise: true)
            let color = UIColor(red: CGFloat(55/255), green: CGFloat(37/255), blue: CGFloat(23/255), alpha: CGFloat(1))
         //   path.fill()
            color.setStroke()
            path.lineWidth = CGFloat(10)
            path.stroke()
//            setNeedsDisplay()
        }
    }

    override func drawRect(rect: CGRect) {
            self.opaque = false;
          drawSmallCircles()


    }

但仍然是相同的黑色,

我做了你说的,我在颜色上调用了setStock方法,我将颜色值更改为255之间的

1 个答案:

答案 0 :(得分:1)

如果您参考initWithRed:green:blue:alpha:的文档,则会指出这些文档应为0.01.0之间的值。

此外,您永远不会致电setStroke来设置线条颜色。您调用color.setStroke()来设置颜色,然后调用path.stroke()以先前指定的颜色绘制笔划。

-

您可能需要以下内容:

func  drawSmallCircles(){
    for oneArray in points {
        let startAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[0]))
        let endAngleRadiant: CGFloat = degreesToRadians(Double(oneArray[1]))
        let radius: CGFloat = 100.0
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngleRadiant, endAngle: endAngleRadiant, clockwise: true)
        let color = UIColor(red: 55.0/255.0, green: 37.0/255.0, blue: 23.0/255.0, alpha: 1.0)
        color.setStroke()
        path.lineWidth = CGFloat(10)
        path.stroke()
    }
}

请注意,这是一种非常暗的颜色,所以它可能看起来很黑。您可能希望减轻它(使用接近1.0的几个值)以更清楚地看到颜色。