Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ContentView.swift
- // StackOverflowSwitUI
- //
- // Created by V15a1 on 2021-07-08.
- //
- import SwiftUI
- struct ContentView: View {
- @State var didScrollUp: Bool = false
- var body: some View {
- NavigationView {
- ScrollView {
- ZStack {
- VStack(alignment:.leading) {
- VStack {
- TagLineView(isHStacked: $didScrollUp.animation())
- }
- .padding()
- ZStack(alignment:.leading) {
- Color(#colorLiteral(red: 0.937254902, green: 0.937254902, blue: 0.937254902, alpha: 1))
- .edgesIgnoringSafeArea(.all)
- .cornerRadius(30.0)
- VStack(alignment: .leading) {
- Text("Popular")
- .font(.custom("PlayfairDisplay-Bold", size: 24))
- .padding(.leading,27)
- .padding(.top,20)
- ScrollView (.horizontal, showsIndicators: false) {
- LazyVStack {
- ForEach(0..<100) { x in
- Text("\(x): Cell")
- .frame(width: 200)
- }
- }
- }
- .padding(.bottom)
- }
- }
- }.background(GeometryReader {
- Color.clear.preference(key: ViewOffsetKey.self,
- value: -$0.frame(in: .named("scroll")).origin.y)
- })
- .onPreferenceChange(ViewOffsetKey.self) { didScrollUp = $0 > 5 ? true : false }
- }
- }
- .coordinateSpace(name: "scroll")
- }
- }
- }
- struct ViewOffsetKey: PreferenceKey {
- typealias Value = CGFloat
- static var defaultValue = CGFloat.zero
- static func reduce(value: inout Value, nextValue: () -> Value) {
- value += nextValue()
- }
- }
- struct TagLineView: View {
- @Binding var isHStacked: Bool
- var body: some View {
- Text("Hello")
- .font(.title)
- .fontWeight(.bold) + Text(isHStacked ? " " : "\n") + // Adding a newline or adding a space based on the orientation of the stack
- Text("Welcome!")
- .font(.title)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement