[SwiftUI Tutorials] SwiftUI Essentials - Creating and Combining Views

     

     

    Creating and Combining Views | Apple Developer Documentation

    This tutorial guides you through building Landmarks — an app for discovering and sharing the places you love. You’ll start by building the view that shows a landmark’s details.

    developer.apple.com

     

    folder structure

    - SwiftUI ์•ฑ ์ˆ˜๋ช… ์ฃผ๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์•ฑ์€ ์•ฑ ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๋Š” ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง.

    - ์ด ๊ตฌ์กฐ์˜ body ์†์„ฑ์€ ํ•˜๋‚˜ ์ด์ƒ์˜ Scene์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ‘œ์‹œํ•  ์ฝ˜ํ…์ธ ๋ฅผ ์ œ๊ณต.

    - @main ์†์„ฑ์€ ์•ฑ์˜ entry point๋ฅผ ์‹๋ณ„.

    - ๊ธฐ๋ณธ์ ์œผ๋กœ SwiftUI view ํŒŒ์ผ์€ ๋‘ ๊ฐœ์˜ ๊ตฌ์กฐ๋ฅผ ์„ ์–ธ

    - ์ฒซ ๋ฒˆ์งธ ๊ตฌ์กฐ๋Š” View ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•˜๊ณ  ๋ทฐ์˜ ์ฝ˜ํ…์ธ ์™€ ๋ ˆ์ด์•„์›ƒ์„ ์„ค๋ช…

    - ๋‘ ๋ฒˆ์งธ ๊ตฌ์กฐ๋Š” ํ•ด๋‹น ๋ณด๊ธฐ์— ๋Œ€ํ•œ ๋ฏธ๋ฆฌ๋ณด๊ธฐ๋ฅผ ์„ ์–ธ

     

    ๋ณด๋ฉด ํŠœํ† ๋ฆฌ์–ผ ๊ณต์‹ ๋ฌธ์„œ๋ž‘ ๋‚ด ์ฝ”๋“œ๋ž‘ ๋‹ค๋ฅธ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๊ฑด xcode๊ฐ€ ์—…๋ฐ์ดํŠธ ๋˜๋ฉด์„œ ๊ธฐ๋ณธ ์ฝ”๋“œ๊ฐ€ ์•„์ด์ฝ˜์ด ์ถ”๊ฐ€๋œ ui๋กœ ๋ฐ”๋€๋“ฏ ํ•˜๋‹ค. 

    ๋”ฑํžˆ ํด๋”๊ตฌ์กฐ์—์„œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์—†์–ด์„œ ์˜์•„ํ–ˆ์—ˆ๋Š”๋ฐ, ๋“ ๋“ ํ•œ ์šฐ๋ฆฌํŒ€ ํ…Œํฌ ๋™๋ฃŒ ๋ฃจ๋น…์ด SF Symbols์˜ ์กด์žฌ๋ฅผ ์•Œ๋ ค์คฌ๋‹ค! 

    Image(systemName: "์•„์ด์ฝ˜ ์ด๋ฆ„") ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋กœ์ปฌ์—์„œ ๋”ฐ๋กœ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์—†์–ด๋„ ์ด๋ฏธ์ง€ ์‚ฝ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค. 

    SwiftUI๊ฐ€ ์ •๋ง ๊ฐ„ํŽธํ•˜๊ณ  ์ข‹์€๊ฒŒ, ๋’ค์— .์„ ๋ถ™์—ฌ์„œ ํ™•์žฅ์ž์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๋””์ž์ธ ์š”์†Œ๋‚˜ ์†์„ฑ๊ฐ’์„ ๋ฐ”๋กœ๋ฐ”๋กœ ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ .

    ์†์—๋งŒ ์ต์œผ๋ฉด UI ์ •๋ง ๋นจ๋ฆฌ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์„ ๋“ฏ! ์•„์ง์€ ์™ธ์šธ๊ฒŒ ๋งŽ๋‹ค. 

     

    ์ด ์•ฑ์„ ๊น”๋ฉด iOS์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ์‹ฌ๋ณผ๋“ค์˜ ์ด๋ฆ„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

     

    - ์ฝ”๋“œ ๋ณ€๊ฒฝ ํ˜น์€ ์ธ์ŠคํŽ™ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ text view ์ˆ˜์ •

    - Landmarks ์•ฑ์„ ๋นŒ๋“œํ•  ๋•Œ source editor, ์บ”๋ฒ„์Šค, ์ธ์ŠคํŽ™ํ„ฐ์™€ ๊ฐ™์€ ํŽธ์ง‘๊ธฐ ์กฐํ•ฉ์„ ์‚ฌ์šฉ

    - ์‚ฌ์šฉํ•˜๋Š” ๋„๊ตฌ์— ๊ด€๊ณ„์—†์ด ์ฝ”๋“œ๋Š” ์—…๋ฐ์ดํŠธ๋œ ์ƒํƒœ๋กœ ์œ ์ง€๋œ๋‹ค.

     

    1) ์ธ์ŠคํŽ™ํ„ฐ 

    ์ฝ”๋“œ์—์„œ inspector ์—ด๋ ค๋ฉด control+option ๋ˆ„๋ฅด๊ณ  ํด๋ฆญํ•˜๋ฉด ๋œ๋‹ค

    - SwiftUI ๋ณด๊ธฐ๋ฅผ ์‚ฌ์šฉ์ž ์ง€์ •ํ•˜๋ ค๋ฉด modifers ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ.

    - modifers๋Š” ๋ทฐ๋ฅผ ๋ž˜ํ•‘ํ•˜์—ฌ ํ‘œ์‹œ ๋˜๋Š” ๊ธฐํƒ€ ์†์„ฑ์„ ๋ณ€๊ฒฝ.

    - ๊ฐ modifer๋Š” ์ƒˆ๋กœ์šด view๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ์—ฌ๋Ÿฌ modifers๋ฅผ ์‚ฌ์Šฌ์ฒ˜๋Ÿผ ์—ฎ์–ด์„œ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ 

     

     

     

    Stack์„ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐ€์ง€ view(UI์š”์†Œ ์ธ๋“ฏ) ๊ฒฐํ•ฉ

     

    HStack์ด ๊ฐ€๋กœ๋กœ, VStack์ด ์„ธ๋กœ 

    ์ฝ”๋“œ์— ๋ฐ”๋กœ ๋Œ๊ณ ์˜ฌ ์ˆ˜ ์žˆ์Œ

     

    ์˜ค flutter ๋ฌธ๋ฒ•์ด๋ž‘ ๋น„์Šทํ•˜๋‹ค

     

     

     

     

    //
    //  SwiftUIView.swift
    //  Landmarks
    //
    //  Created by ๊น€ํ•˜์€ on 2023/03/26.
    //
    
    import SwiftUI
    
    struct CircleImage: View {
        var body: some View {
            Image("turtlerock")
                .clipShape(Circle())
                .overlay {
                   Circle().stroke(.white, lineWidth: 4)
                }
                .shadow(radius: 7)
        }
    }
    
    struct CircleImage_Previews: PreviewProvider {
        static var previews: some View {
            CircleImage()
        }
    }

    MapKit์˜ Map view๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€๋„๋ฅผ ๋ Œ๋”๋ง

     

    - @State ํŠน์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‘˜ ์ด์ƒ์˜ ๋ณด๊ธฐ์—์„œ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์•ฑ์˜ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•œ ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์†Œ์Šค๋ฅผ ์„ค์ •

    - SwiftUI๋Š” ๊ธฐ๋ณธ ์Šคํ† ๋ฆฌ์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ  ๊ฐ’์— ๋”ฐ๋ผ ๋ทฐ๋ฅผ ์ž๋™์œผ๋กœ ์—…๋ฐ์ดํŠธํ•จ

     

    //
    //  ContentView.swift
    //  Landmarks
    //
    //  Created by ๊น€ํ•˜์€ on 2023/03/26.
    //
    
    import SwiftUI
    
    struct ContentView: View {
        var body: some View {
            VStack {
                MapView()
                    .ignoresSafeArea(edges: .top)
                    .frame(height: 300)
                CircleImage()
                    .offset(y: -130)
                    .padding(.bottom, -130)
                
                VStack(alignment: .leading) {
                    Text("Turtle Rock")
                        .font(.title)
                        .foregroundColor(.black)
                    HStack {
                        Text("Joshua Tree National Park")
                            .font(.subheadline)
                        Spacer()
                        Text("California")
                            .font(.subheadline)
                    }
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                    
                    Divider()
                    
                    Text("About Turtle Rock")
                       .font(.title2)
                    Text("Descriptive text goes here.")
                }
                .padding()
                Spacer()
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€