[SwiftUI] メニュースタイルのpickerを作る

    @State private var selectedColor = 0
    
    var body: some View {
        
        VStack {
            Picker(selection: $selectedColor, label: Text("Color")){
                Text("Red").tag(0)
                Text("Green").tag(1)
                Text("Blue").tag(2)
            }
        }
        .padding()        
    }

選択した色を表示する
L アイテムの並び順はtag()で指定する

struct ContentView: View {
    @State private var selectedColor = 0
    let colorViews:[Color] = [.red, .green, .blue]
    let colorNames = ["Red", "Green", "Blue"]
    
    var body: some View {
        VStack {
            Picker(selection: $selectedColor, label: Text("Color")){
                Text("Red").tag(0)
                Text("Green").tag(1)
                Text("Blue").tag(2)
            }
            
            HStack {
                colorViews[selectedColor]
                    .frame(width: 50, height:100)
                Text("value: \(selectedColor)").frame(width:60)
                Text("\(colorNames[selectedColor])").frame(width:70)
            }
        }
        .padding()
    }
}

segmented style

            Picker(selection: $selectedColor, label: Text("Color")){
                Text("Red").tag(0)
                Text("Green").tag(1)
                Text("Blue").tag(2)
            }.pickerStyle(.segmented)

処理の分岐

 VStack {
            Picker(selection: $selectedColor, label: Text("Color")){
                Text("Red").tag(0)
                Text("Green").tag(1)
                Text("Blue").tag(2)
            }
            .pickerStyle(.segmented)
            .frame(width: 250, height: 30)
            .padding()
            
            let color = colorViews[selectedColor]
            switch color {
            case .red:
                Rectangle()
                    .frame(width: 50, height: 50)
                    .foregroundColor(.red)
            case .green:
                Circle()
                    .frame(width: 50, height: 50)
                    .foregroundColor(.green)
            case .blue:
                Circle()
                    .stroke(lineWidth: 8)
                    .frame(width: 50, height: 50)
                    .foregroundColor(.blue)
            default:
                Text("default")
            }

Switch文

func fortune(color: String){
    switch color {
    case "red", "yellow":
        print("\(color)は、当たり")
    case "green":
        print("\(color)は、大当たり")
    default:
        print("\(color)は、ハズレ")
    }
}

fortune(color: "yellow")
fortune(color: "blue")
fortune(color: "green")
fortune(color: "red")

segmented pickerはカッコいいですね^^