Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

[SwiftUI] GeometryReader ๋ณธ๋ฌธ

apple/๐Ÿš SwiftUI & Combine

[SwiftUI] GeometryReader

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2022. 5. 19. 00:49

GeometryReader

 

โœ… GeometryReader์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

GeometryReader๋Š” ์‰ฝ๊ฒŒ ๋งํ•ด์„œ ๋””๋ฐ”์ด์Šค ๊ธฐ๊ธฐ๋งˆ๋‹ค ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

์Œ,, ๋Œ€์ถฉ ์˜คํ†  ๋ ˆ์ด์•„์›ƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹์•„!

 

โœ… ์ฝ”๋“œ

import SwiftUI

enum Index { // enum์œผ๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.
    case one, two, three
}

struct MyGeometryReaderVStack : View {
    
    @State var index: Index = .one // ์ดˆ๊ธฐ ์ƒํƒœ๋Š” one์œผ๋กœ
    
    var body: some View{
        
        // ๋ถ€๋ชจ๋ทฐ์˜ ๋น„์œจ์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
        GeometryReader{ geometry in 
            VStack{
                
                Button(action:{
                    // ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„๋•Œ ๋กœ์ง
                    print("1๋ฒˆ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
                    
                    withAnimation { // ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ฑธ๊ธฐ ์œ„ํ•จ.
                        self.index = .one // index๋ฅผ 1๋กœ ๋ฐ”๊พผ๋‹ค. 
                    }
                    
                }){
                    // ๋ฒ„ํŠผ์˜ ์ƒ๊น€์ƒˆ
                    Text("1")
                    .font(.largeTitle)
                    .fontWeight(.black)
                    .frame(width: 100, height: geometry.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: geometry.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: geometry.size.height / 3)
                    .padding(.horizontal, self.index == .three ? 50 : 0)
                    .foregroundColor(Color.white)
                    .background(Color.green)
                }
            }
        }
        .background(Color.yellow)
        .edgesIgnoringSafeArea(.all)
        

    }
}

์ด๋ ‡๊ฒŒ ๋ฐ”๋€Œ๋Š”๋ฐ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์ ์šฉ๋œ๋‹ค.

 

 

SwiftUI์—์„œ ์ฝ”๋“œ ์ ‘๋Š” ํŽด๋Š” ๋ฐฉ๋ฒ• 

 - ์•ŒํŠธ ์ปค๋ฉ˜ํŠธ ์™ผ์ชฝ(์˜ค๋ฅธ์ชฝ)

 

โœ… GeometryProxy position

์ด๊ฑด ์™œ ์‚ฌ์šฉํ•˜๋ƒ๋ฉด ์ ˆ๋Œ€์ ์ธ ํฌ์ง€์…˜์„ ์žก๊ธฐ ์œ„ํ•จ์ด๋‹ค.

GeometryReader๋Š” "๋ทฐ์˜ ์ขŒํ‘œ์— ์ด๋ฆ„์„ ์ •ํ•ด์„œ ๋‹ค๋ฅธ ์ฝ”๋“œ์—์„œ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๋Š” ๋ฉ”์„œ๋“œ"

GeometryProxy๋Š” "์ปจํ…Œ์ด๋„ˆ ๋ทฐ์˜ ์ขŒํ‘œ๋‚˜ ํฌ๊ธฐ๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ"

 

์ฆ‰, GeometryReader๋Š” ๊ธฐํ•˜ํ•™์ ์ธ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ทฐ ์ž์ฒด์ด๊ณ  ํ•ด๋‹น ์ •๋ณด๋ฅผ ์–ป์–ด๋‚ด๊ธฐ ์œ„ํ•ด์„  GeometrryProxy๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด์ฃ .

import SwiftUI

enum Index {
    case one, two, three
}

struct MyGeometryReaderVStack : View {
    
    @State var index : Index = .one
    
    // ์ง€์˜ค๋ฉ”ํŠธ๋ฆฌ ํ”„๋ก์‹œ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๊ณ  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 { proxy in
            
            VStack {
                Button(action: {
                    // ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜์—ˆ์„๋•Œ ๋กœ์ง
                    print("1๋ฒˆ์ด ํด๋ฆญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")
                    
                    withAnimation {
                        self.index = .one
                    }
                    
                }){
                    // ๋ฒ„ํŠผ์˜ ์ƒ๊น€์ƒˆ
                    Text("1")
                    .font(.largeTitle)
                    .fontWeight(.black)
                    .frame(width: 100, height: proxy.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: proxy.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: proxy.size.height / 3)
//                    .padding(.horizontal, self.index == .three ? 50 : 0)
//                    .foregroundColor(Color.white)
//                    .background(Color.green)
//                }
            }
//            .position(CGPoint(x: proxy.frame(in: .local).midX, y: proxy.frame(in: .local).midY))
            .position(centerPosition(proxy))
            
        }
        .background(Color.yellow)
        .edgesIgnoringSafeArea(.all)
        

    }
}

 

ํ”„๋ก์‹œ๋กœ ์„ธํŒ…

 

 

(์ฐธ๊ณ )

https://fomaios.tistory.com/entry/SwiftUI-GeometryReader%EB%9E%80?category=933741 

 

[SwiftUI] GeometryReader๋ž€? (feat. GeometryProxy,CoordinateSpace)

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ’ป ์ž…๋‹ˆ๋‹ค! ์˜ค๋Š˜์€ SwitUI๋ฅผ ์ด์šฉํ•ด์„œ Layout์„ ์žก์„ ๋•Œ ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” GeometryReader์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~ GeometryReader๋ž€? ์ด๋ฆ„์„ ์ง์—ญํ•˜๋ฉด Geometry๋Š”

fomaios.tistory.com

 

'apple > ๐Ÿš SwiftUI & Combine' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[SwiftUI 3.0] State/ Binding / EnvironmentObject  (0) 2022.05.19
[SwiftUI] TabView + CustomTabView  (0) 2022.05.19
[SwiftUI] NavigationView  (0) 2022.05.18
[SwiftUI] List (tableView in UIKit)  (0) 2022.05.18
[SwiftUI] Alert  (0) 2022.05.18
Comments