์ด๋ฒ ํฌ์คํ ์์๋ ์ผ์ ์ ํฌ์คํ ํ๋ ๋ฉ๋ชจ์ฑ์์ ์ฌ์ฉํ๋ BottomSheet์ ๋ํด์ ์์ฑํด๋ณด๋ ค๊ณ ํ๋ค.
๋ฉ๋ชจ๋ฅผ ์ถ๊ฐ, ์ญ์ , ์์ ํ์ ๋๋ง๋ค ํ๋จ์์ ํด๋น ์์ ์ ์ฑ๊ณตํ๋ค๋ ์๋ฆผ์ ๋ ธ์ถํ๋ ๊ฒ์ด ๋ชฉ์ ์ด์๋ค.
๋ฐ๋ก ์์๋ณด์.
BottomSheetType
์์ ๋งํ ๊ฒ์ฒ๋ผ ์ถ๊ฐ, ์ญ์ , ์์ ์ธ ๊ฐ์ง๋ฅผ ์๋ ค์ผ ํ๊ธฐ ๋๋ฌธ์ ์ด์ ๋ง๋ ํ ์คํธ๋ค์ด ํ์ํ๋ค.
enum BottomSheetType {
enum BottomSheetResult {
case fail
case success
var description: String {
switch self {
case .fail:
return "์คํจํ์ต๋๋ค :("
case .success:
return "์ฑ๊ณตํ์ต๋๋ค :)"
}
}
}
case update(BottomSheetResult)
case delete(BottomSheetResult)
case add(BottomSheetResult)
var title: String {
switch self {
case .update:
return "๋ฉ๋ชจ ์
๋ฐ์ดํธ ์๋ด"
case .delete:
return "๋ฉ๋ชจ ์ญ์ ์๋ด"
case .add:
return "๋ฉ๋ชจ ์ถ๊ฐ ์๋ด"
}
}
var message: String {
switch self {
case .update(let type):
return "์
๋ฐ์ดํธ์ \(type.description)"
case .delete(let type):
return "์ญ์ ์ \(type.description)"
case .add(let type):
return "์๋ก์ด ๋ฉ๋ชจ ์ถ๊ฐ์ \(type.description)"
}
}
}
ํด๋น ์์ ์ ์คํจํ๋์ง ์ฑ๊ณตํ๋์ง ์๋ ค์ค ์ ์๊ฒ ๋ง๋ค์๋ค.
๊ทธ๋ค์ง ์ค์ํ์ง ์๋ค.
๋ค์.
BottomSheet View
์ผ๋จ ํ๋จ์ ๋ณด์ด๊ฒ๋ ์๋ฆผ๊ณผ ๋ค์ ๋ฐฑ๊ทธ๋ผ์ด๋๊ฐ ํ์ํ๋ค.
๋ฐ๋ผ์ ZStack์ ์ด์ฉํด์ Color๋ฅผ ํตํด ๋ฐฑ๊ทธ๋ผ์ด๋๋ฅผ ๋จผ์ ๋ฃ๊ณ , ์๋ฆผ ๋ฉ์์ง๊ฐ ์๋ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ธฐ๋ก ํ๋ค.
struct BottomSheet: View {
@Binding var isShowing: Bool
@Binding var bottomSheetType: BottomSheetType
var body: some View {
ZStack(alignment: .bottom) {
if isShowing {
Color.black
.opacity(0.3)
.ignoresSafeArea()
.onTapGesture { isShowing.toggle() }
BottomSheetMessageView(bottomSheetType: $bottomSheetType)
.setRoundedCorner(radius: 12)
.transition(.move(edge: .bottom))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
.ignoresSafeArea()
.animation(.easeInOut, value: isShowing)
}
}
BottomSheet์ ์์๋ทฐ์์ ์ฌ์ฉํ ์์ ์ด๊ธฐ ๋๋ฌธ์ ์์๋ทฐ๊ฐ ๊ฐ์ง๊ณ ์๋ ํ๋กํผํฐ๋ค์ Binding ํด์ ์ฌ์ฉํ๋ค. ๊ฐ๋ น ๋ฉ์์ง๊ฐ ์ถ๊ฐ๋์๋ค๋ ๊ฒ์ ์์๋ทฐ๋ง ์๊ณ ์๊ธฐ ๋๋ฌธ์ BottomSheetType์ Binding ํด "์ถ๊ฐ"์ ๊ด๋ จ๋ ๋ด์ฉ์ผ๋ก ์ฑ์ฐ๊ธฐ ์ํด์๋ค.
BottomSheetMessageView
์์ ๋ณด์ด๋ ์ค์ ์๋ฆผ ๋ด์ฉ์ ๊ฐ์ง๊ณ ์๋ ๋ทฐ๋ค.
struct BottomSheetMessageView: View {
@Binding var bottomSheetType: BottomSheetType
var body: some View {
VStack(spacing: 8) {
Text(bottomSheetType.title)
.font(.system(size: 18, weight: .bold))
.foregroundStyle(.black)
Text(bottomSheetType.message)
.font(.system(size: 16, weight: .regular))
.foregroundStyle(.black)
}
.frame(maxWidth: .infinity, minHeight: 150)
.background(.white)
}
}
BottomSheet์์ Binding๋ ๊ฐ์ ์ ๋ฌ๋ฐ์ ์ฌ์ฉํ๋ค.
์ฌ์ฉ๋ฐฉ๋ฒ
๊ฐ๋จํ๋ค. ZStack์ผ๋ก ๊ตฌ์ฑ๋ ์์๋ทฐ์์ ํ๋จ์ ์์ฑํ๊ณ @State๋ก ์ ์ธ๋ ํ๋กํผํฐ๋ง ์ ๋ฐ์ธ๋ฉํด์ ์ฌ์ฉํ๋ฉด ๋๋ค.
@State private var isShowingBottomSheet = false
@State private var bottomSheetType: BottomSheetType = .add(.success)
var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
ZStack {
NavigationStack {
List {
...
}
}
}
...
BottomSheet(isShowing: $isShowingBottomSheet, bottomSheetType: $bottomSheetType)
}
}
}
๋ ์ข์ ๋ฐฉ๋ฒ์ด ์๊ฑฐ๋ ์๋ชป๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋๊ธ๋ก ์๋ ค์ฃผ์๋ฉด ๊ฐ์ฌํ๊ฒ ์ต๋๋ค.
๊ทธ๋ผ ์ด๋ง.
'iOS > SwiftUI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SwiftUI] ๊ฐ๋จํ ๋ฉ๋ชจ์ฑ์ ๋ง๋ค๋ฉด์ ์์๊ฐ๋ณด์ ( feat.TCA ) (0) | 2024.01.18 |
---|