首页 > 解决方案 > Swift - 创建自定义颜色数组

问题描述

有没有办法创建一组自定义颜色?我知道我可以创建自定义颜色,class UIColors但就我而言,我需要一组自定义颜色。

我想实现这样的目标:

let listColors: [UIColor] = [
    let color1 = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1),
    let color2 = UIColor(displayP3Red: 2, green: 2, blue: 2, alpha: 1),
    let color3 = UIColor(displayP3Red: 3, green: 3, blue: 3, alpha: 1),
]

标签: iosswiftuicolor

解决方案


或者,如果您希望能够按名称数组索引引用它们:

let color1 = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), //White
let color2 = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0), //Gray
let color3 = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0), //Red

let listColors: [UIColor] = [color1, color2, color3]

推荐阅读