23.12.18 TIL SwiftUI 21. How to use if-else and conditional statements

Hay·2023년 12월 18일
0

SwiftUI_Beginner

목록 보기
17/19

If else 로직을 이용해서 코딩하기

import SwiftUI

struct ConditionalBootcamp: View {
    
    @State var showCircle: Bool = false
    @State var showRectangle: Bool = false
    @State var isLoding: Bool = false
    
    var body: some View {
        VStack(spacing: 20) {
            
						//로딩버튼
            Button("is loading: \(isLoding.description)".uppercased()) {
                isLoding.toggle()
            }
            
            if isLoding {
                ProgressView()
            }

//-----------------------------------------------------------            
            //shapes 버튼
            Button("Circle Button: \(showCircle.description)") {
                showCircle.toggle() //.toggle()은 true/false를 바꿔준다
            }
            
            Button("Rectangle Button: \(showRectangle.description)") {
                showRectangle.toggle()
            }
            
            //show the circle only if showCircle is true
            //adding some logic
            //if showCircle == true 이라고 쓰는 대신, 
						//if showCircle {}이라고 간단하게 작성할 수도 있다.
            //if showCircle == false 이라고 쓰는 대신, 
						//if !showCircle {}이라고 간단히 쓸 수 있다
            if showCircle {
                Circle()
                    .frame(width: 100, height: 100)
            }
            
            if showRectangle {
                Rectangle()
                    .frame(width: 100, height: 100)
            } 
            
            if showCircle || showRectangle {
                RoundedRectangle(cornerRadius: 25.0)
                    .frame(width: 200, height: 100)
            }
            
            Spacer()
            
        }
    }
}

#Preview {
    ConditionalBootcamp()
}

0개의 댓글