Skip to content

Commit 3a2f012

Browse files
author
Trent Guillory
committed
code cleanup and simplification
1 parent d1f2e40 commit 3a2f012

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

SnapToScrollDemo/Sources/ContentView.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ struct ContentView: View {
1010
var body: some View {
1111
VStack {
1212

13-
HStackSnap {
13+
HStackSnap(leadingOffset: 16) {
1414

1515
ForEach(items, id: \.1) { item in
1616

1717
Text(item.0)
1818
.font(.largeTitle)
19-
.padding()
19+
.padding(16)
2020
.overlay(GeometryReaderOverlay(id: item.1))
2121
}
2222
}
23+
// HStackSnap(snapLocation: .center) {
24+
//
25+
// ForEach(items, id: \.1) { item in
26+
//
27+
// Text(item.0)
28+
// .font(.largeTitle)
29+
// .padding()
30+
// .overlay(GeometryReaderOverlay(id: item.1))
31+
// }
32+
// }
2333
}
2434

2535
/// SnapHStack {

Sources/HStackSnap.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ public struct HStackSnap<Content: View>: View {
55

66
// MARK: Lifecycle
77

8-
public init(@ViewBuilder content: @escaping () -> Content) {
8+
public init(
9+
leadingOffset: CGFloat,
10+
coordinateSpace: String = "SnapToScroll",
11+
@ViewBuilder content: @escaping () -> Content) {
912

1013
self.content = content
14+
targetOffset = leadingOffset
15+
scrollOffset = leadingOffset
16+
self.coordinateSpace = coordinateSpace
1117
}
1218

1319
// MARK: Public
1420

1521
public var body: some View {
1622

1723
GeometryReader { geometry in
24+
1825
ScrollView(.horizontal) {
1926

2027
HStack(content: content)
@@ -27,11 +34,12 @@ public struct HStackSnap<Content: View>: View {
2734

2835
itemFrames[pref.id] = pref
2936
}
37+
3038
})
3139
.disabled(true)
3240
.gesture(snapDrag)
3341
}
34-
.coordinateSpace(name: ContentPreferenceKey.coordinateSpace)
42+
.coordinateSpace(name: coordinateSpace)
3543
}
3644

3745
// MARK: Internal
@@ -66,7 +74,7 @@ public struct HStackSnap<Content: View>: View {
6674
scrollOffset += distanceToTarget(
6775
x: closestFrame.rect.minX)
6876
}
69-
77+
7078
prevScrollOffset = scrollOffset
7179
}
7280
}
@@ -79,13 +87,15 @@ public struct HStackSnap<Content: View>: View {
7987
// MARK: Private
8088

8189
/// Current scroll offset.
82-
@State private var scrollOffset: CGFloat = 0
83-
90+
@State private var scrollOffset: CGFloat
91+
8492
/// Stored offset of previous scroll, so scroll state is resumed between drags.
8593
@State private var prevScrollOffset: CGFloat = 0
86-
87-
///
88-
@State private var targetOffset: CGFloat = 0
94+
95+
/// Calculated offset based on `SnapLocation`
96+
@State private var targetOffset: CGFloat
8997

9098
@State private var itemFrames: [UUID: ContentPreferenceData] = [:]
99+
100+
private let coordinateSpace: String
91101
}

Sources/Model/ContentPreferenceKey.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ struct ContentPreferenceKey: PreferenceKey {
1919

2020
static var defaultValue: [ContentPreferenceData] = []
2121

22-
static var coordinateSpace: String = "ContentPreferenceKey"
23-
2422
static func reduce(
2523
value: inout Value,
2624
nextValue: () -> Value) {

Sources/Model/SnapLocation.swift

Lines changed: 0 additions & 16 deletions
This file was deleted.

Sources/Views/GeometryReaderOverlay.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@ import SwiftUI
33

44
public struct GeometryReaderOverlay: View {
55

6-
let id: UUID
7-
8-
public init(id: UUID) {
9-
6+
// MARK: Lifecycle
7+
8+
public init(id: UUID, coordinateSpace: String = "SnapToScroll") {
9+
1010
self.id = id
11+
self.coordinateSpace = coordinateSpace
1112
}
12-
13+
14+
// MARK: Public
15+
1316
public var body: some View {
14-
17+
1518
GeometryReader { geometry in
16-
19+
1720
Rectangle().fill(Color.blue.opacity(0.5))
1821
.preference(
1922
key: ContentPreferenceKey.self,
2023
value: [ContentPreferenceData(
2124
id: id,
22-
rect: geometry.frame(in: .named(ContentPreferenceKey.coordinateSpace)))])
25+
rect: geometry.frame(in: .named(coordinateSpace)))])
2326
}
2427
}
28+
29+
// MARK: Internal
30+
31+
let id: UUID
32+
let coordinateSpace: String
2533
}

0 commit comments

Comments
 (0)