ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • #12-1. 지오메트리 프록시 위치 잡는 3가지 방법
    SwiftUI 앱만들기 2022. 7. 7. 17:51

    import SwiftUI

     

     

     

    enum Index{

        

        case one, two, three

    }

     

    struct MyGeometryReaderVStack: View {

     

        @State var index: Index = .one

        

        

        //#3. 중앙에 위치하기... 지오메트리 프록시를 매개변수로 CGPoint를 반환하는 클로져

        let centerPosition : (GeometryProxy) -> CGPoint = { proxy in

            return CGPoint(x: proxy.frame(in: .local).midX, y: proxy.frame(in: .local).midY)

            

        }

        

        

        

        

        var body: some View {

            

            GeometryReader { geomtry in

                

                VStack(spacing:0) {

                    

                    Button(action:{

                        

                        withAnimation{

                            self.index = .one

                        }

                        //버튼이 클릭되었을때 로직

                        print("1")

                        

                    }){

                        //버튼의 생김새

                        Text("1")

                            .font(.largeTitle)

                            .fontWeight(.black)

                            .frame(width:100, height: geomtry.size.height / 3)

                            .padding(.horizontal, self.index == .one ? 50 : 0)

                            .foregroundColor(Color.white)

                            .background(Color.red)

                    }

                    

     

     

                    Button(action:{

                        //버튼이 클릭되었을때 로직

                        print("2")

                        withAnimation{

                            self.index = .two

                        }

                    }){

                        //버튼의 생김새

                        Text("2")

                            .font(.largeTitle)

                            .fontWeight(.black)

                            .frame(width:100, height: geomtry.size.height / 3)

                            .padding(.horizontal, self.index == .two ? 50 : 0)

                            .foregroundColor(Color.white)

                            .background(Color.blue)

                    }

     

                    Button(action:{

                        //버튼이 클릭되었을때 로직

                        print("3")

                        withAnimation{

                            self.index = .three

                        }

                    }){

                        //버튼의 생김새

                        Text("3")

                            .font(.largeTitle)

                            .fontWeight(.black)

                            .frame(width:100, height: geomtry.size.height / 3)

                            .padding(.horizontal, self.index == .three ? 50 : 0)

                            .foregroundColor(Color.white)

                            .background(Color.green)

                    }

     

                 

                } //VStack

                

                //#1. 중앙에 위치하기 ...유연한 방법(OS에 맞게)

    //                .frame(width: geomtry.size.width, height: geomtry.size.height, alignment: .center)

                

                //#2. 중앙에 위치하기 ...

    //            .position(CGPoint(x: geomtry.frame(in: .local).midX, y: geomtry.frame(in: .local).midY))

                

                //#3. 받아오기

                .position(centerPosition(geomtry))

                

                

            }.background(Color.yellow)

                .edgesIgnoringSafeArea(.all)

            

            

     

        }

    }

     

    struct MyGeometryReaderVStack_Previews: PreviewProvider {

        static var previews: some View {

            MyGeometryReaderVStack()

        }

    }

     

    'SwiftUI 앱만들기' 카테고리의 다른 글

    #14. 커스텀 탭뷰(CustomTabView)  (0) 2022.07.08
    #13. 탭뷰(TabView)  (0) 2022.07.07
    #12. 지오메트리리더(GeometryReader)  (0) 2022.07.07
    #11. 내비게이션(Navigation) 뷰  (0) 2022.07.07
    #10. 목록띄우기  (0) 2022.07.07

    댓글

Designed by Tistory.