From 1159ca5bac63cef49208b38e2fd8382336466f3f Mon Sep 17 00:00:00 2001 From: Mercen Date: Wed, 12 Jul 2023 19:47:08 +0900 Subject: [PATCH] ADD: ScrollView --- .../Component/Example/ExampleView.swift | 7 +-- .../Component/ScrollView/ScrollView.swift | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 Sources/OpenTDS/Component/ScrollView/ScrollView.swift diff --git a/Sources/OpenTDS/Component/Example/ExampleView.swift b/Sources/OpenTDS/Component/Example/ExampleView.swift index 90eb785..d964771 100644 --- a/Sources/OpenTDS/Component/Example/ExampleView.swift +++ b/Sources/OpenTDS/Component/Example/ExampleView.swift @@ -2,11 +2,8 @@ import SwiftUI struct ExampleView: View { var body: some View { - TossTabView { - Text("a") - .tossTabItem("홈", Image(systemName: "house.fill")) - Text("b") - .tossTabItem("마이", Image(systemName: "person.fill")) + TossScrollView("전체") { + Text("AnyView") } } } diff --git a/Sources/OpenTDS/Component/ScrollView/ScrollView.swift b/Sources/OpenTDS/Component/ScrollView/ScrollView.swift new file mode 100644 index 0000000..c96a931 --- /dev/null +++ b/Sources/OpenTDS/Component/ScrollView/ScrollView.swift @@ -0,0 +1,60 @@ +import SwiftUI + +/** + Dynamic Scroll View of Toss. + + TossScrollView("Home") { + AnyView() + } + */ +@available(macOS 11, iOS 14, *) +public struct TossScrollView: View { + + @State var shrink: Bool = false + let title: String + let showsIndicators: Bool + let content: Content + + /** + - Parameters: + - title: Title of the View. + - showsIndicators: To show indicators or not. + */ + public init(_ title: String, + showsIndicators: Bool = true, + @ViewBuilder content: @escaping () -> Content) { + self.title = title + self.showsIndicators = showsIndicators + self.content = content() + } + + public var body: some View { + VStack { + Text(title) + .font(.system(size: 17, weight: .medium)) + .opacity(shrink ? 1 : 0) + .padding(.vertical, 12) + .frame(maxWidth: .infinity) + GeometryReader { outsideProxy in + ScrollView(showsIndicators: showsIndicators) { + VStack(spacing: 36) { + GeometryReader { insideProxy in + Text(title) + .font(.system(size: 26, weight: .bold)) + .padding(.leading, 24) + .onChange(of: insideProxy.frame(in: .global).minY) { newValue in + DispatchQueue.main.async { + let proxy = outsideProxy.frame(in: .global).minY - newValue + withAnimation(.default) { + shrink = proxy > 36 + } + } + } + } + content + } + } + } + } + } +}