Skip to content

Commit 4e866ee

Browse files
Merge pull request #1 from swiftui-library/feat/snap-to-center
Feat/snap to center
2 parents dea316f + 40a9169 commit 4e866ee

File tree

16 files changed

+632
-170
lines changed

16 files changed

+632
-170
lines changed

README.md

+26-17
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,56 @@ https://user-images.githubusercontent.com/8763719/131393666-6af82d2a-998e-4dba-a
66

77
## Getting Started
88

9-
Using `SnapToScroll` is straightforward. There's only two requirements:
9+
Using `SnapToScroll` is straightforward. There's just three steps.
1010

11-
1. Replace `HStack` with `HStackSnap`
12-
2. Add `GeometryReaderOverlay` to your view.
11+
1. Import `SnapToScroll`
12+
2. Replace `HStack` with `HStackSnap`
13+
3. Add `.snapAlignmentHelper` to your view.
1314

1415
An example:
1516

1617
```swift
18+
import SnapToScroll // Step 1
19+
...
1720

18-
HStackSnap(leadingOffset: 16) {
21+
HStackSnap(alignment: .center(32)) { // Step 2
1922

20-
ForEach(modelArray) { viewModel in
23+
ForEach(myModels) { viewModel in
2124

22-
myView(viewModel: viewModel)
23-
.frame(maxWidth: 250)
24-
.overlay(GeometryReaderOverlay(id: viewModel.id))
25-
}
26-
}
25+
MyView(
26+
selectedIndex: $selectedIndex,
27+
viewModel: viewModel
28+
)
29+
.snapAlignmentHelper(id: viewModel.id) // Step 3
30+
}
2731
}
2832

2933
```
3034
For more examples, see `SnapToScrollDemo/ContentView.swift`.
3135

32-
## Options
36+
## Configuration
3337

3438
`HStackSnap` comes with two customizable properties:
3539

36-
- `leadingOffset`: The leading padding you'd like each element to snap to.
37-
- `coordinateSpace`: Option to set custom name for the coordinate space, in the case you're using multiple `HStackSnap`s of various sizes.
40+
- `alignment`: The way you'd like your elements to be arranged.
41+
- `leading(CGFloat)`: Aligns your child views to the leading edge of `HStackSnap`. This configuration supports elements of various sizes, so long as they don't take up all available horizontal space (which would extend beyond the screen). Use the value to set the size of the left offset.
42+
- `center(CGFloat)`: Automatically aligns your child view to the center of the screen, using the offset value you've provided. This is accomplished with inside of the `.snapAlignmentHelper` which sets the frame width based on the available space. Note that setting your own width elsewhere may produce unexpected layouts.
43+
- `coordinateSpace`: Option to set custom name for the coordinate space, in the case you're using multiple `HStackSnap`s of various sizes. If you use this, set the same value in `.snapAlignmentHelper`.
44+
45+
`.snapAlignmentHelper` comes with two options as well:
46+
47+
- `id`: Required. A unique ID for the element.
48+
- `coordinateSpace`: Same as above.
3849

3950
## Limitations
4051

41-
1. If your child views are designed to take up all available horizontal space, they'll expand beyond the visible view. Prevent this with `.frame(maxWidth: x)`
42-
2. `HStackSnap` is currently designed to work with static content.
43-
3. At the moment, `HStackSnap` offers snapping to the leading edge only. If you'd like to offer a PR that adds support for `.center` and / or `.trailing`, I'd love to look it over!
52+
- `HStackSnap` is currently designed to work with static content.
4453

4554
## How it Works
4655

4756
At render, `HStackSnap` reads the frame data of each child element and calculates the `scrollOffset` each element should use. Then, on `DragGesture.onEnded`, the nearest snap location is calculated, and the scroll offset is set to this point.
4857

49-
Read through `HStackSnap` for more details.
58+
Read through `HStackSnap.swift` and `Views/HStackSnapCore.swift` for more details.
5059

5160
## Credits
5261

SnapToScrollDemo/SnapToScrollDemo.xcodeproj/project.pbxproj

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
840130AF26DFB8DD00E4A8A3 /* GettingStartedModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840130AE26DFB8DC00E4A8A3 /* GettingStartedModel.swift */; };
11+
840130B126DFB93400E4A8A3 /* GettingStartedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840130B026DFB93400E4A8A3 /* GettingStartedView.swift */; };
12+
840130B326DFC27700E4A8A3 /* Example3ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840130B226DFC27700E4A8A3 /* Example3ContentView.swift */; };
13+
840130B526DFC2FB00E4A8A3 /* Example2ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840130B426DFC2FB00E4A8A3 /* Example2ContentView.swift */; };
14+
840130B726DFC33100E4A8A3 /* Example1ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 840130B626DFC33100E4A8A3 /* Example1ContentView.swift */; };
1015
841B0F8326DD39A4008A436B /* TripTupleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841B0F8226DD39A4008A436B /* TripTupleView.swift */; };
1116
84B568D926DD271000D37CF2 /* TagView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B568D826DD271000D37CF2 /* TagView.swift */; };
1217
84B568DB26DD309400D37CF2 /* Example1HeaderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B568DA26DD309400D37CF2 /* Example1HeaderView.swift */; };
@@ -23,6 +28,11 @@
2328
/* End PBXBuildFile section */
2429

2530
/* Begin PBXFileReference section */
31+
840130AE26DFB8DC00E4A8A3 /* GettingStartedModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GettingStartedModel.swift; sourceTree = "<group>"; };
32+
840130B026DFB93400E4A8A3 /* GettingStartedView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GettingStartedView.swift; sourceTree = "<group>"; };
33+
840130B226DFC27700E4A8A3 /* Example3ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Example3ContentView.swift; sourceTree = "<group>"; };
34+
840130B426DFC2FB00E4A8A3 /* Example2ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Example2ContentView.swift; sourceTree = "<group>"; };
35+
840130B626DFC33100E4A8A3 /* Example1ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Example1ContentView.swift; sourceTree = "<group>"; };
2636
841B0F8226DD39A4008A436B /* TripTupleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TripTupleView.swift; sourceTree = "<group>"; };
2737
84B568D826DD271000D37CF2 /* TagView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagView.swift; sourceTree = "<group>"; };
2838
84B568DA26DD309400D37CF2 /* Example1HeaderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Example1HeaderView.swift; sourceTree = "<group>"; };
@@ -52,9 +62,20 @@
5262
/* End PBXFrameworksBuildPhase section */
5363

5464
/* Begin PBXGroup section */
65+
840130AD26DFB8C800E4A8A3 /* Example3 */ = {
66+
isa = PBXGroup;
67+
children = (
68+
840130B226DFC27700E4A8A3 /* Example3ContentView.swift */,
69+
840130AE26DFB8DC00E4A8A3 /* GettingStartedModel.swift */,
70+
840130B026DFB93400E4A8A3 /* GettingStartedView.swift */,
71+
);
72+
path = Example3;
73+
sourceTree = "<group>";
74+
};
5575
84B568D626DD26A800D37CF2 /* Example1 */ = {
5676
isa = PBXGroup;
5777
children = (
78+
840130B626DFC33100E4A8A3 /* Example1ContentView.swift */,
5879
84C99CD726D99BA100C1D5C4 /* TagModel.swift */,
5980
84B568D826DD271000D37CF2 /* TagView.swift */,
6081
84B568DA26DD309400D37CF2 /* Example1HeaderView.swift */,
@@ -68,6 +89,7 @@
6889
84B568DD26DD351900D37CF2 /* TripModel.swift */,
6990
84B568DF26DD37A600D37CF2 /* TripView.swift */,
7091
841B0F8226DD39A4008A436B /* TripTupleView.swift */,
92+
840130B426DFC2FB00E4A8A3 /* Example2ContentView.swift */,
7193
);
7294
path = "Example 2";
7395
sourceTree = "<group>";
@@ -97,6 +119,7 @@
97119
84D9FA3526D9753600F87EF5 /* AppDelegate.swift */,
98120
84B568D626DD26A800D37CF2 /* Example1 */,
99121
84B568DC26DD311F00D37CF2 /* Example 2 */,
122+
840130AD26DFB8C800E4A8A3 /* Example3 */,
100123
84D9FA3726D9753600F87EF5 /* SceneDelegate.swift */,
101124
84D9FA3926D9753600F87EF5 /* ContentView.swift */,
102125
);
@@ -204,10 +227,15 @@
204227
files = (
205228
84B568DB26DD309400D37CF2 /* Example1HeaderView.swift in Sources */,
206229
84D9FA3626D9753600F87EF5 /* AppDelegate.swift in Sources */,
230+
840130AF26DFB8DD00E4A8A3 /* GettingStartedModel.swift in Sources */,
207231
841B0F8326DD39A4008A436B /* TripTupleView.swift in Sources */,
232+
840130B726DFC33100E4A8A3 /* Example1ContentView.swift in Sources */,
208233
84B568D926DD271000D37CF2 /* TagView.swift in Sources */,
234+
840130B326DFC27700E4A8A3 /* Example3ContentView.swift in Sources */,
209235
84B568E026DD37A600D37CF2 /* TripView.swift in Sources */,
236+
840130B526DFC2FB00E4A8A3 /* Example2ContentView.swift in Sources */,
210237
84B568DE26DD351900D37CF2 /* TripModel.swift in Sources */,
238+
840130B126DFB93400E4A8A3 /* GettingStartedView.swift in Sources */,
211239
84D9FA3826D9753600F87EF5 /* SceneDelegate.swift in Sources */,
212240
84D9FA3A26D9753600F87EF5 /* ContentView.swift in Sources */,
213241
84C99CD826D99BA100C1D5C4 /* TagModel.swift in Sources */,

SnapToScrollDemo/Sources/ContentView.swift

+11-38
Original file line numberDiff line numberDiff line change
@@ -5,55 +5,28 @@ import SwiftUI
55

66
struct ContentView: View {
77

8-
@State var items = [("one", 1), ("two", 2), ("three", 3), ("four", 4), ("five", 5), ("six", 6)]
9-
108
var body: some View {
119
VStack {
1210

1311
ScrollView {
1412

1513
VerticalSpace
1614

17-
VStack {
18-
19-
LargeHeader(text: "Example 1")
20-
21-
Example1HeaderView()
22-
23-
// Don't forget to attach GeometryReaderOverlay!
24-
HStackSnap(leadingOffset: 16) {
25-
26-
ForEach(TagModel.exampleModels) { viewModel in
27-
28-
TagView(viewModel: viewModel)
29-
.overlay(GeometryReaderOverlay(id: viewModel.id))
30-
}
31-
}.padding(.top, 4)
32-
}
15+
LargeHeader(text: "Example 1")
16+
17+
Example1ContentView()
3318

3419
VerticalSpace
3520

36-
VStack {
37-
38-
LargeHeader(text: "Example 2")
39-
40-
Text("Explore Nearby")
41-
.font(.system(size: 22, weight: .semibold, design: .rounded))
42-
.frame(maxWidth: .infinity, alignment: .leading)
43-
.padding([.top, .leading], 16)
44-
45-
HStackSnap(leadingOffset: 16) {
46-
47-
ForEach(TripTupleModel.exampleModels) { viewModel in
21+
LargeHeader(text: "Example 2")
22+
23+
Example2ContentView()
24+
25+
VerticalSpace
4826

49-
TripTupleView(viewModel: viewModel)
50-
.frame(maxWidth: 250)
51-
.overlay(GeometryReaderOverlay(id: viewModel.id))
52-
}
53-
}
54-
.frame(height: 200)
55-
.padding(.top, 4)
56-
}
27+
LargeHeader(text: "Example 3")
28+
29+
Example3ContentView()
5730
}
5831
}
5932
.preferredColorScheme(.light)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import SwiftUI
2+
import SnapToScroll
3+
4+
// MARK: - Example2ContentView
5+
6+
struct Example2ContentView: View {
7+
var body: some View {
8+
9+
VStack {
10+
11+
Text("Explore Nearby")
12+
.font(.system(size: 22, weight: .semibold, design: .rounded))
13+
.frame(maxWidth: .infinity, alignment: .leading)
14+
.padding([.top, .leading], 16)
15+
16+
HStackSnap(alignment: .leading(16)) {
17+
18+
ForEach(TripTupleModel.exampleModels) { viewModel in
19+
20+
TripTupleView(viewModel: viewModel)
21+
.frame(maxWidth: 250)
22+
.snapAlignmentHelper(id: viewModel.id)
23+
}
24+
} onSwipe: { index in
25+
26+
print(index)
27+
}
28+
.frame(height: 130)
29+
.padding(.top, 4)
30+
}
31+
}
32+
}
33+
34+
// MARK: - Example2ContentView_Previews
35+
36+
struct Example2ContentView_Previews: PreviewProvider {
37+
static var previews: some View {
38+
Example2ContentView()
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Example2ContentView.swift
3+
// Example2ContentView
4+
//
5+
// Created by Trent Guillory on 9/1/21.
6+
//
7+
8+
import SwiftUI
9+
import SnapToScroll
10+
11+
struct Example1ContentView: View {
12+
var body: some View {
13+
14+
VStack {
15+
16+
Example1HeaderView()
17+
18+
// Don't forget to attach snapAlignmentHelper!
19+
HStackSnap(alignment: .leading(16)) {
20+
21+
ForEach(TagModel.exampleModels) { viewModel in
22+
23+
TagView(viewModel: viewModel)
24+
.snapAlignmentHelper(id: viewModel.id)
25+
}
26+
}.padding(.top, 4)
27+
}
28+
}
29+
}
30+
31+
struct Example1ContentView_Previews: PreviewProvider {
32+
static var previews: some View {
33+
Example2ContentView()
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import SnapToScroll
2+
import SwiftUI
3+
4+
// MARK: - Example3ContentView
5+
6+
struct Example3ContentView: View {
7+
8+
// MARK: Internal
9+
10+
var body: some View {
11+
12+
VStack {
13+
14+
Text("Getting Started")
15+
.font(.system(size: 22, weight: .semibold, design: .rounded))
16+
.foregroundColor(.white)
17+
.frame(maxWidth: .infinity, alignment: .leading)
18+
.padding([.top, .leading], 32)
19+
20+
HStackSnap(alignment: .center(32)) {
21+
22+
ForEach(GettingStartedModel.exampleModels) { viewModel in
23+
24+
GettingStartedView(
25+
selectedIndex: $selectedGettingStartedIndex,
26+
viewModel: viewModel)
27+
.snapAlignmentHelper(id: viewModel.id)
28+
}
29+
} onSwipe: { index in
30+
31+
selectedGettingStartedIndex = index
32+
}
33+
.frame(height: 200)
34+
.padding(.top, 4)
35+
}
36+
.padding([.top, .bottom], 64)
37+
.background(LinearGradient(
38+
colors: [Color("Cream"), Color("LightPink")],
39+
startPoint: .top,
40+
endPoint: .bottom))
41+
}
42+
43+
// MARK: Private
44+
45+
@State private var selectedGettingStartedIndex: Int = 0
46+
}
47+
48+
// MARK: - Example3ContentView_Previews
49+
50+
struct Example3ContentView_Previews: PreviewProvider {
51+
static var previews: some View {
52+
Example3ContentView()
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Foundation
2+
3+
struct GettingStartedModel: Identifiable {
4+
5+
static let exampleModels: [GettingStartedModel] = [
6+
.init(
7+
id: 0,
8+
systemImage: "camera.aperture",
9+
title: "Snap a Pic",
10+
body: "We feature the viewfinder front and center in this throwback app."),
11+
.init(
12+
id: 1,
13+
systemImage: "camera.filters",
14+
title: "Filter it Up",
15+
body: "Add filters - from detailed colorization to film effects."),
16+
.init(
17+
id: 2,
18+
systemImage: "paperplane",
19+
title: "Send It",
20+
body: "Share your photos with your contacts. Or the entire world."),
21+
.init(
22+
id: 3,
23+
systemImage: "sparkles",
24+
title: "Be Awesome",
25+
body: "You're clearly already doing this. Just wanted to remind you. 😉"),
26+
]
27+
28+
let id: Int
29+
let systemImage: String
30+
let title: String
31+
let body: String
32+
}

0 commit comments

Comments
 (0)