How can I make a Swift enum with UIColor value?

前端 未结 9 749
深忆病人
深忆病人 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:10

    This can be done much more succinctly (and should):

    extension UIColor
    {
        static let myColor = UIColor(displayP3Red: 0.0, green: 0.7, blue: 0.0, alpha: 1.0)
    }
    

    (Any other method that returns a UIColor is equally suitable, doesn't need to be displayP3Red)

    Usage:

    let someColor: UIColor = .myColor
    

提交回复
热议问题