Advertisement
Guest User

Untitled

a guest
Sep 21st, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.65 KB | None | 0 0
  1. //
  2. //  ContentView.swift
  3. //  StackOverflowSwitUI
  4. //
  5. //  Created by V15a1 on 2021-07-08.
  6. //
  7.  
  8. import SwiftUI
  9.  
  10. struct ContentView: View {
  11.  
  12.     @State var didScrollUp: Bool = false
  13.     var body: some View {
  14.         NavigationView {
  15.             ScrollView {
  16.                 ZStack {
  17.                     VStack(alignment:.leading) {
  18.                         VStack {
  19.                             TagLineView(isHStacked: $didScrollUp.animation())
  20.                         }
  21.                         .padding()
  22.                         ZStack(alignment:.leading) {
  23.                             Color(#colorLiteral(red: 0.937254902, green: 0.937254902, blue: 0.937254902, alpha: 1))
  24.                                 .edgesIgnoringSafeArea(.all)
  25.                                 .cornerRadius(30.0)
  26.                             VStack(alignment: .leading) {
  27.                                 Text("Popular")
  28.                                     .font(.custom("PlayfairDisplay-Bold", size: 24))
  29.                                     .padding(.leading,27)
  30.                                     .padding(.top,20)
  31.  
  32.                                 ScrollView (.horizontal, showsIndicators: false) {
  33.                                     LazyVStack {
  34.                                         ForEach(0..<100) { x in
  35.                                             Text("\(x): Cell")
  36.                                                 .frame(width: 200)
  37.                                         }
  38.                                     }
  39.                                 }
  40.                                 .padding(.bottom)
  41.  
  42.                             }
  43.  
  44.  
  45.                         }
  46.  
  47.                     }.background(GeometryReader {
  48.                         Color.clear.preference(key: ViewOffsetKey.self,
  49.                                                value: -$0.frame(in: .named("scroll")).origin.y)
  50.                     })
  51.                     .onPreferenceChange(ViewOffsetKey.self) { didScrollUp = $0 > 5 ? true : false }
  52.                 }
  53.  
  54.             }
  55.             .coordinateSpace(name: "scroll")
  56.         }
  57.  
  58.     }
  59. }
  60.  
  61. struct ViewOffsetKey: PreferenceKey {
  62.     typealias Value = CGFloat
  63.     static var defaultValue = CGFloat.zero
  64.     static func reduce(value: inout Value, nextValue: () -> Value) {
  65.         value += nextValue()
  66.     }
  67. }
  68.  
  69.  
  70. struct TagLineView: View {
  71.  
  72.     @Binding var isHStacked: Bool
  73.  
  74.     var body: some View {
  75.         Text("Hello")
  76.             .font(.title)
  77.             .fontWeight(.bold) + Text(isHStacked ? " " : "\n") +  // Adding a newline or adding a space based on the orientation of the stack
  78.             Text("Welcome!")
  79.             .font(.title)
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement