2016-06-12 189 views
0

我想用我的自定义颜色填充UIBezierPath,但只有默认颜色(如UIColor.redColorUIColor.blueColor)正在工作。为什么我不能用自定义UIColor填充UIBezierPath?

这里是我的代码:

override func drawRect(rect: CGRect) { 

    let layerHeight = self.layer.frame.height 
    let layerWidth = self.layer.frame.width 
    let width:CGFloat = 50 

    let bezierPath = UIBezierPath() 
    bezierPath.moveToPoint(CGPointMake(layerWidth, layerHeight)) 
    bezierPath.addLineToPoint(CGPointMake(layerWidth, layerHeight - width)) 
    bezierPath.addLineToPoint(CGPointMake(layerWidth - width, layerHeight)) 
    bezierPath.closePath() 

    // UIColor.blueColor().setFill() works, but a custom color does not: 
    UIColor(colorLiteralRed: 23, green: 34, blue: 200, alpha: 1).setFill() 
    bezierPath.fill() 
} 

回答

1

您正在使用错误的价值观。 The color values have to be in range 0.0 - 1.0

UIColor(colorLiteralRed: 0.2, green: 0.3, blue: 0.9, alpha: 1) 

然后,我会建议使用稍有不同的初始化,而不是:

UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0) 

什么在你的代码之前发生的是1.0以上的所有值都被视为1.0,因此一起形成白色,你可能没有看到填充任何东西。

+0

哦,我的上帝太尴尬,但谢谢你:) –

+0

@JulianPomper恭喜你的应用程序顺便说一句,看起来非常整齐,设计很好! – luk2302

+0

非常感谢!但这是一个旧版本,我目前正在研究看起来好多了的新版本,我认为!你是怎么找到它的? –

相关问题