๐Ÿ“ฑ App Development Study/iOS๐ŸŽ

[Udemy iOS & Swift Bootcamp] SwiftUI Layout Design - Spacers, Subviews (SwiftUI ๋กœ ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ์•ฑ ๋งŒ๋“ค๊ธฐ)

ibelieveinme 2023. 7. 12. 16:59
728x90

๊ธฐ์กด์— ๋งŒ๋“ค์—ˆ๋˜ Dice ์•ฑ์„ SwiftUI ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋‹ค์‹œ ๋งŒ๋“ค์–ด๋ณด์ž.

 

1. ์ด๋ฏธ์ง€ Asset ์ถ”๊ฐ€

https://drive.google.com/uc?export=download&id=1IubA1ewlMRNRh9JKDAT4DY4fMbZONX9j 

image asset์„ ๋‹ค์šด๋ฐ›์•„์„œ Assets ํŒŒ์ผ์— import ํ•ด์ค€๋‹ค.

๊ฐ ๋‹จ๋ง๊ธฐ์— ๋งž๊ฒŒ ์•Œ์•„์„œ ๋งž์ถฐ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” image ๋“ค...

 

 

2. ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ์ถ”๊ฐ€ํ•˜๊ธฐ

1) ContentView ์˜ ์ดˆ๊ธฐ ๋ชจ์Šต์— ๋ฐฐ๊ฒฝ image ์ฝ”๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ.

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ContentView ์˜ default ๋ชจ์Šต

import SwiftUI

struct ContentView: View {
    var body: some View {
        
        ZStack {
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(.all)
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

ZStack ๊ณผ Image๋ฅผ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต

 

2) ์ถ”๊ฐ€ UI ๋ฐฐ์น˜ํ•˜๊ธฐ

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(.all)
            VStack{
                Image("diceeLogo")
                Spacer()
                HStack{
                    DiceView(n: 1)
                    DiceView(n: 1)
                }
                .padding(.horizontal)
                Spacer()
                Button(action:{

                }){
                    Text("Roll")
                        .font(.system(size: 50))
                        .fontWeight(.heavy)
                        .foregroundColor(.white)
                        .padding(.horizontal)
                }
                .background(Color.red)
            }
        }

    }
}

struct DiceView: View{
    let n: Int
    var body: some View{
        Image("dice\(n)")
            .resizable()
            .aspectRatio(1, contentMode: .fit)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

 

3) ๋ฒ„ํŠผ์— ๊ธฐ๋Šฅ ์ถ”๊ฐ€

import SwiftUI

struct ContentView: View {
    
    @State var leftDiceNumber = 1
    @State var rightDiceNumber = 1
    
    var body: some View {
        ZStack {
            Image("background")
                .resizable()
                .edgesIgnoringSafeArea(.all)
            VStack{
                Image("diceeLogo")
                Spacer()
                HStack{
                    DiceView(n: leftDiceNumber)
                    DiceView(n: rightDiceNumber)
                }
                .padding(.horizontal)
                Spacer()
                Button(action:{
                    self.leftDiceNumber = Int.random(in: 1...6)
                    self.rightDiceNumber = Int.random(in: 1...6)
                }){
                    Text("Roll")
                        .font(.system(size: 50))
                        .fontWeight(.heavy)
                        .foregroundColor(.white)
                        .padding(.horizontal)
                }
                .background(Color.red)
            }
        }

    }
}

struct DiceView: View{
    let n: Int
    var body: some View{
        Image("dice\(n)")
            .resizable()
            .aspectRatio(1, contentMode: .fit)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


cf) @State ์†์„ฑ ์•Œ์•„๋ณด๊ธฐ

๋ณ€์ˆ˜๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๋งˆ๋‹ค ๋ณ€์ˆ˜๊ฐ’, ContentView ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

 

https://developer.apple.com/documentation/swiftui/state

 

State | Apple Developer Documentation

A property wrapper type that can read and write a value managed by SwiftUI.

developer.apple.com

 

728x90