์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- designpattern
- ํจ์คํธ์บ ํผ์ค
- raywenderlich
- XCTest
- tableView
- reactorkit
- BFS
- Kuring
- RxSwift
- realm
- swift
- MVVM
- ๋ฐฑ์ค
- ํ๋ก๊ทธ๋๋จธ์ค
- ios
- Lv2
- Flutter
- TCA
- Xcode
- rxcocoa
- BOJ
- CollectionView
- combine
- arkit
- SnapKit
- SwiftUI
- UIKit
- visionOS
- node.js
- Swfit
- Today
- Total
lgvv98
[SwiftUI] GeometryReader ๋ณธ๋ฌธ
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 |