How can I make a Swift enum with UIColor value?

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

    If you want to return multiple value then use below code...it's absolutely working for me....

    enum GetDriverStatus : String {
        case ClockIn            = "Clock In"
        case TripStart          = "Trip Start"
        case BeaconTouchPlant   = "Beacon Touch Plant"
        case PickUp             = "Pick Up"
        case BeaconTouchSite    = "Beacon Touch Site"
        case BeaconLeftSite     = "Beacon Left Site"
        case DropOff            = "Drop Off"
        case BreakIn            = "Break In"
        case BreakOut           = "Break Out"
        case TripEnd            = "Trip End"
        case DayEnd             = "Day End"
        //case ClockOut           = "Clock Out"
    
        //Get data from ID
        static var allValues: [GetDriverStatus] {
            return [
                .ClockIn,
                .TripStart,
                .BeaconTouchPlant,
                .PickUp,
                .BeaconTouchSite,
                .BeaconLeftSite,
                .DropOff,
                .BreakIn,
                .BreakOut,
                .TripEnd,
                .DayEnd
            ]
        }
    
        //Get Color
        var colorAndStatus: (UIColor,String) {
            get {
                switch self {
                case .ClockIn,.TripStart: //Idle
                    return (UIColor(red: 248/255, green: 39/255, blue: 71/255, alpha: 1.0),"Idle") //dark pink-red
                case .BeaconTouchPlant,.PickUp:
                    return (UIColor(red: 46/255, green: 180/255, blue: 42/255, alpha: 1.0),"Picking up") //Green
                case .BeaconTouchSite:
                    return (UIColor(red: 252/255, green: 172/255, blue: 0/255, alpha: 1.0),"On site") //orange
                case .DropOff,.BeaconLeftSite:
                    return (UIColor(red: 12/255, green: 90/255, blue: 255/255, alpha: 1.0),"Dropping off") //blue
                case .BreakIn,.BreakOut:
                    return (UIColor(red: 151/255, green: 151/255, blue: 151/255, alpha: 1.0),"On break") //warm-grey-two
                case .TripEnd:
                    return (UIColor.black,"Trip end")
                case .DayEnd:
                    return (UIColor.black,"Done for the day")
                }
            }
        }
    } 
    

    How to use this code Passing .allvalues["index of your option"] you getting UIColor at 0 position as well as String value as 1 position

    GetDriverStatus.allValues[1].colorAndStatus.0 //UIColor.Black
    GetDriverStatus.allValues[2].colorAndStatus.1 //"Picking up"
    

提交回复
热议问题