【问题标题】:Wrong color when convert UIColor to CGColor [duplicate]将 UIColor 转换为 CGColor 时颜色错误 [重复]
【发布时间】:2017-08-11 12:30:22
【问题描述】:

当我将 UIColor 转换为 CGColor 时,我没有得到正确的颜色。

这个颜色:

 let color = UIColor(red: 198, green: 35.0, blue: 80.0, alpha: 1.0)

当我将它应用到 CAlayer 上时

layer.fillColor = UIColor.red.cgColor
layer.strokeColor = color.cgColor

会在我的手机上出现white

如你所见,圆圈是白色的...

【问题讨论】:

  • 198/255.0 等每个组件。目标值在0到1之间,而不是0到255之间。这是因为color开头写错了。

标签: ios swift swift3 uicolor cgcolor


【解决方案1】:

UIKit 中的颜色 是使用 介于 0 和 1 中的 float 而不是 int 从 0 到 255 指定的,因此您需要除以将所有 RGB 值提高 255.0。

let color = UIColor(red: 198.0/255.0, green: 35.0/255.0, blue: 80.0/255.0, alpha: 1.0)

【讨论】:

    猜你喜欢
    • 2020-04-28
    • 2018-08-31
    • 2015-05-06
    • 2017-04-07
    • 2019-11-21
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多