@State var iLike = true var body: some View { VStack { Toggle(isOn: $iLike){ Text("Like or Not") .font(.largeTitle) } .fixedSize() .padding(50) if iLike { Image(systemName: "heart.fill") .font(.system(size: 80)) .foregroundColor(.red) } else { Image(systemName: "heart.slash") .font(.system(size: 80)) .foregroundColor(.gray) } } }
ボタンタイプのtoggleスイッチ
@State var isShow = true var body: some View { VStack { Toggle(isOn: $isShow){ Label(isShow ? "消す " : "表示する", systemImage: isShow ? "eye.slash" : "eye") .frame(width: 150) } .toggleStyle(.button) .tint(isShow ? .red : .indigo) .clipShape(Capsule()) .animation(.easeInOut(duration: 0.2), value:isShow) Image("eigakan") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 300) .opacity(isShow ? 1.0 : 0.0) .animation(.easeInOut(duration: 1.0), value:isShow) } }
トグルスイッチの色指定
@State var isYaruki = true @State var isAuto = true var body: some View { VStack(alignment: .leading) { Toggle("やる気モード", isOn: $isYaruki) .tint(.yellow) Toggle("自動設定", isOn: $isAuto) .tint(.red) } .frame(width: 200) }
Buttonでトグルボタン
@State var isFast = true var body: some View { HStack { Button(action: { withAnimation { isFast.toggle() } }){ Text(isFast ? "RUN" : "STOP") .font(.title) .foregroundColor(isFast ? .red : .black) .padding(.trailing) .frame(width:100) Image(systemName: isFast ? "hare": "tortoise") .foregroundColor(isFast ? .blue : .green) .rotationEffect(.degrees(isFast ? 0 : 180)) .scaleEffect(3) .frame(width: 100) }.frame(width: 240, height: 70) } }
手軽に使えるSF Symbols
HStack { Image(systemName: "sun.max") Image(systemName: "cloud.rain") Image(systemName: "camera") Image(systemName: "message") Image(systemName: "a.circle") Image(systemName: "calendar") Image(systemName: "star.fill") .frame(width: 34, height: 34) .foregroundColor(Color.yellow) .background(Color.green) .clipShape(Circle()) } .imageScale(.large)
条件分岐
func hantei(tokuten: Int) -> String { var result = "結果: \(tokuten)" if (tokuten >= 80) { result += " -> 合格・優秀" } else if(tokuten >= 60){ result += " -> 合格" } else { result += " -> 不合格" } return result } let test1 = hantei(tokuten: 67) let test2 = hantei(tokuten: 82) let test3 = hantei(tokuten: 56) print(test1) print(test2) print(test3)
条件式を論理演算子を使って作る
func hantei(sugaku:Int, eigo:Int) -> String { var result = "数学: \(sugaku), 英語:\(eigo)" if(sugaku >= 80) && (eigo >= 80){ result += " -> 合格" } else { result += " -> 不合格" } return result } let test1 = hantei(sugaku: 97, eigo: 68) let test2 = hantei(sugaku: 81, eigo: 83) let test3 = hantei(sugaku: 72, eigo: 65) print(test1) print(test2) print(test3)
ここら辺は特にOKやな