File tree Expand file tree Collapse file tree 2 files changed +34
-9
lines changed
Expand file tree Collapse file tree 2 files changed +34
-9
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,8 @@ struct DetailsView: View {
8484 @State var refreshed = 0
8585 var style : Style
8686 @State var useImage = true
87+ let rectangles = ( 0 ..< 50 ) . map { _ in CGFloat . random ( in: 10 ..< 50 ) }
88+
8789 var body : some View {
8890 ScrollView {
8991 VStack {
@@ -94,6 +96,25 @@ struct DetailsView: View {
9496 }
9597 Text ( " Details! " )
9698 Text ( " Refreshed: \( refreshed) " )
99+
100+ ScrollView ( . horizontal, showsIndicators: false ) {
101+ LazyHStack ( spacing: 8 ) {
102+ ForEach ( 0 ..< 10 ) { _ in
103+ Rectangle ( )
104+ . frame ( width: 100 , height: 100 )
105+ }
106+ }
107+ . padding ( . horizontal)
108+ }
109+ LazyVGrid ( columns: Array ( repeating: GridItem ( . flexible( ) , spacing: 8 ) , count: 3 ) ) {
110+ ForEach ( rectangles, id: \. self) { size in
111+ VStack {
112+ Rectangle ( )
113+ . frame ( width: size, height: size)
114+ Text ( " text " )
115+ }
116+ }
117+ }
97118 }
98119 }
99120 . refresher ( style: style) {
Original file line number Diff line number Diff line change @@ -3,19 +3,23 @@ import SwiftUI
33
44struct OffsetReader : View {
55 var onChange : ( CGFloat ) -> ( )
6- @State private var frame = CGRect ( )
76
87 public var body : some View {
98 GeometryReader { geometry in
10- Spacer ( minLength: 0 )
11- . onChange ( of: geometry. frame ( in: . global) ) { value in
12- if value. integral != self . frame. integral {
13- DispatchQueue . main. async {
14- self . frame = value
15- onChange ( value. minY)
16- }
17- }
9+ Color . clear
10+ . preference ( key: OffsetPreferenceKey . self,
11+ value: geometry. frame ( in: . global) . minY)
12+ . onPreferenceChange ( OffsetPreferenceKey . self) { offset in
13+ onChange ( offset)
1814 }
1915 }
2016 }
2117}
18+
19+ private struct OffsetPreferenceKey : PreferenceKey {
20+ static var defaultValue = CGFloat . zero
21+
22+ static func reduce( value: inout CGFloat , nextValue: ( ) -> CGFloat ) {
23+ value = nextValue ( )
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments