How can I make a Swift enum with UIColor value?

前端 未结 9 748
深忆病人
深忆病人 2020-12-16 09:37

I\'m making a drawing app and I would like to refer to my colors through use of an enum. For example, it would be cleaner and more convenient to use Colors.RedColor

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 10:16

    If your color isn't one of those defined by UIColor's convenience method, you can add an extension to UIColor:

    extension UIColor {
        static var firstColor: UIColor  { return UIColor(red: 1, green: 0, blue: 0, alpha: 1) }
        static var secondColor: UIColor { return UIColor(red: 0, green: 1, blue: 0, alpha: 1) }
    }
    
    // Usage
    let myColor = UIColor.firstColor
    

提交回复
热议问题