Advertisement
Guest User

Untitled

a guest
Dec 18th, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.79 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct TimestampedRect: Equatable {
  4.     var date: Date
  5.     var rect: CGRect
  6. }
  7.  
  8. struct MyTextPreferenceKey: PreferenceKey {
  9.     typealias Value = TimestampedRect
  10.    
  11.     static var defaultValue: TimestampedRect = TimestampedRect(date: Date(), rect: .zero)
  12.    
  13.     static func reduce(value: inout TimestampedRect, nextValue: () -> TimestampedRect) {
  14.         let next = nextValue()
  15.        
  16.         if (next.date > value.date) {
  17.             value = next
  18.         }
  19.     }
  20. }
  21.  
  22. struct ContentView : View {
  23.     @State private var rect: CGRect = .zero
  24.    
  25.     var body: some View {
  26.         ZStack(alignment: .topLeading) {
  27.             RoundedRectangle(cornerRadius: 15)
  28.                 .stroke(lineWidth: 3.0)
  29.                 .foregroundColor(Color.green)
  30.                 .frame(width: rect.width, height: rect.height)
  31.                 .offset(x: rect.minX, y: rect.minY)
  32.                 .animation(.easeInOut(duration: 0.5))
  33.            
  34.             VStack {
  35.                 Spacer()
  36.                
  37.                 HStack {
  38.                     MonthView("January")
  39.                     MonthView("February")
  40.                     MonthView("March")
  41.                     MonthView("April")
  42.                 }
  43.                
  44.                 Spacer()
  45.                
  46.                 HStack {
  47.                     MonthView("May")
  48.                     MonthView("June")
  49.                     MonthView("July")
  50.                     MonthView("August")
  51.                 }
  52.                
  53.                 Spacer()
  54.                
  55.                 HStack {
  56.                     MonthView("September")
  57.                     MonthView("October")
  58.                     MonthView("November")
  59.                     MonthView("December")
  60.                 }
  61.                
  62.                 Spacer()
  63.             }
  64.                 .onPreferenceChange(MyTextPreferenceKey.self) { preference in
  65.                     rect = preference.rect
  66.                 }
  67.         }
  68.             .coordinateSpace(name: "main")
  69.     }
  70. }
  71.  
  72. struct MonthView: View {
  73.     @State var date: Date = .distantPast
  74.    
  75.     let label: String
  76.    
  77.     init(_ label: String) {
  78.         self.label = label
  79.     }
  80.    
  81.     var body: some View {
  82.         let preferenceView = MyPreferenceViewSetter(date: date)
  83.        
  84.         Text(label)
  85.             .padding(10)
  86.             .background(preferenceView)
  87.             .onTapGesture {
  88.                 date = Date()
  89.             }
  90.     }
  91. }
  92.  
  93. struct MyPreferenceViewSetter: View {
  94.     let date: Date
  95.    
  96.     var body: some View {
  97.         GeometryReader { geometry in
  98.             Color.clear
  99.                 .preference(key: MyTextPreferenceKey.self,
  100.                             value: TimestampedRect(date: date, rect: geometry.frame(in: .named("main"))))
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement