【问题标题】:Why can't I fill a UIBezierPath with custom UIColor? [duplicate]为什么我不能用自定义 UIColor 填充 UIBezierPath? [复制]
【发布时间】:2016-10-13 05:17:09
【问题描述】:

我想用我的自定义颜色填充UIBezierPath,但只有UIColor.redColorUIColor.blueColor 等默认颜色有效。

这是我的代码:

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()
}

【问题讨论】:

    标签: ios swift


    【解决方案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,因此一起形成白色,您可能没有看到填充任何内容。

    【讨论】:

    • 天哪,太尴尬了,但谢谢你:)
    • @JulianPomper 恭喜您的应用程序 btw,看起来非常整洁且设计精美!
    • 非常感谢!但那是一个旧版本,我目前正在开发新版本,我认为它看起来好多了!你是怎么找到它的:O?
    • @JulianPomper 我查看了您的个人资料,其中有指向其主页的链接。由于我是德国人,我能够理解所有文本。顺便说一句,当在 Impressum 页面上时,页脚中指向 Kontakt 页面的链接已损坏(404);)
    • 啊,好的,谢谢!我会改变的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 2017-03-13
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多