IOS/SwiftUI

(SwiftUI)10.GeometryReader,enum,애니메이션

프린이0218 2021. 4. 28. 13:09
//
//  testGeomerty.swift
//  GeoMertyReader_Tutorial
//
//  Created by devdepot on 2021/04/28.
//

import SwiftUI

enum Index {
    case one,two,three
}


struct testGeomerty: View {
    
    @State var index : Index = .one
    
    var body: some View{
        
        GeometryReader{ rate in
            HStack{
                Spacer()
                VStack(alignment: .center){
                    
                    Button(action: {
                        print("1번이 클릭 되었습니다.")
                        withAnimation{
                            self.index = .one
                        }
                        
                    }, label: {
                        Text("1")
                           .font(.largeTitle)
                            .frame(width: 100,height:rate.size.height/3)
                            .padding(.horizontal,self.index == .one ? 100 : 0)
                           .background(Color.red)
                           .foregroundColor(Color.white)
                    })
                    
                    Button(action: {
                        print("2번이 클릭 되었습니다.")
                        withAnimation{
                            self.index = .two
                        }
                        
                    }, label: {
                        Text("2")
                           .font(.largeTitle)
                           .frame(width: 100,height:rate.size.height/3)
                            .padding(.horizontal,self.index == .two ? 100 : 0)
                           .background(Color.blue)
                           .foregroundColor(Color.white)
                    })
                    
                    Button(action: {
                        print("3번이 클릭 되었습니다.")
                        withAnimation{
                            self.index = .three
                        }
                       
                    }, label: {
                        Text("3")
                           .font(.largeTitle)
                           .frame(width: 100,height:rate.size.height/3)
                            .padding(.horizontal,self.index == .three ? 100 : 0)
                           .background(Color.green)
                           .foregroundColor(Color.white)
                       })
                  
                 
              
            }//vstack
                Spacer()
            }//hstack
            
        }
        .background(Color.yellow)
        .edgesIgnoringSafeArea(.all)
    }
    
}

struct testGeomerty_Previews: PreviewProvider {
    static var previews: some View{
        testGeomerty()
    }
}