2016-06-12 188 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

非常感謝!但這是一箇舊版本,我目前正在研究看起來好多了的新版本,我認爲!你是怎麼找到它的? –

相關問題