๐ก ์ผ๋ฐ์ ์ผ๋ก struct ๋ด์ ๋ณ์๊ฐ ๋ณ๊ฒฝ์ ๋ถ๊ฐ๋ฅํ๋ค.๊ตฌ์กฐ์ฒด ๋ด์ ๋ณ์๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ, View๊ฐ ํ์๋๋ ๋์ ๊ฐ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ ๋ญ๊ฐ ์์๊น?
์ ๋์ฌ๋ก ์ฌ์ฉ๋๋ '@'๋ ์์ฑ๋ํผ(property wrapper)์ด๋ค.
์์ฑ ๋ํผ๋ ์์ฑ์ ํ์ฅ์์ ๋น์ทํ๋ค. ์ด๋ฐ ์์ฑ ๋ํผ๋ฅผ ์ฌ์ฉํด ์์ฑ์ ๊ธฐ๋ฅ ๋ฐ ๋
ผ๋ฆฌ๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
@State private var userName: String = "apple"
Text("์์ด๋: \(apple)")
@State private var password: String = "๋น๋ฐ๋ฒํธ"
TextField("๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํ์ธ์", text: $password)
์๋ฐฉํฅ ๋ฐ์ธ๋ฉ์๋ '$'ํค์๋๋ฅผ ์ด์ฉํ๋ค.
์ฌ์ฉ์๊ฐ ํ
์คํธ๋ฅผ ์
๋ ฅํ ๋๋ง๋ค ์ ๊ฐ์ @State ๋ณ์์ ์ ์ฅ๋๊ณ , ๊ด๋ จ๋ View๋ ๋ค์ ๊ทธ๋ ค์ง๋ค.
๐ก ์ ๋ณด ์์คํ ์ ๋์์ธ ์ด๋ก ์ค ํ๋์ธ, ๋ชจ๋ธ๊ณผ ๊ทธ์ ์ฐ๊ฒฐ๋ ์คํค๋ง๋ฅผ ์ ์ํจ์ ์์ด์ ๋ชจ๋ ๋ฐ์ดํฐ ์์๋ ์์คํ ์์ ์ ์ผํ ๊ฐ์ผ๋ก ์กด์ฌํ๋๋ก ํ๋ ๊ฒ์ ์๋ฏธํ๋ค. - ์ํคํผ๋์
// struct
import SwiftUI
struct ButtonDatas {
var showColors: Bool = false
var animateButton: Bool = false
}
struct Home: View {
@State private var buttonData = ButtonDatas()
var body: some View {...}
@ViewBuilder
func SideBar() -> some View {...}
.padding(.top, 20)
.frame(height: buttonData.showColors ? nil : 0)
.opacity(buttonData.showColors ? 1 : 0)
.zIndex(0)
}
.frame(maxHeight: .infinity, alignment: .top)
.padding(.vertical)
.padding(.horizontal, 22)
.padding(.top, 35)
}
@ViewBuilder
func AddButton() -> some View {
Button {
withAnimation(.interactiveSpring(response: 0.5, dampingFraction: 0.6, blendDuration: 0.6)) {
buttonData.showColors.toggle()
buttonData.animateButton.toggle()
}
// Resetting the button
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
withAnimation(.spring()) {
buttonData.animateButton.toggle()
}
}
} label: {
Image(systemName: "plus")
.font(.title2)
.foregroundColor(.white)
.scaleEffect(buttonData.animateButton ? 1.1 : 1)
.padding(isMacOS() ? 12 : 15)
.background(Color.black)
.clipShape(Circle())
}
.rotationEffect(.init(degrees: buttonData.showColors ? 45 : 0))
.scaleEffect(buttonData.animateButton ? 1.1 : 1)
.padding(.top, 30)
}
}
// class
class TextClass {
var text1: String = ""
var text2: String = ""
init(text1: String, text2: String) {
self.text1 = text1
self.text2 = text2
}
}
struct Home: View {
@State private var textData = TextClass(text1: "์ฒซ๋ฒ์งธ", text2: "๋๋ฒ์งธ")
}