Skip to content

Commit 767112f

Browse files
Merge pull request #7 from chiffre-it/main
Add spacing support between stack items.
2 parents a9763fb + 457fdd7 commit 767112f

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Sources/HStackSnap.swift

+5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ public struct HStackSnap<Content: View>: View {
77

88
public init(
99
alignment: SnapAlignment,
10+
spacing: CGFloat? = nil,
1011
coordinateSpace: String = "SnapToScroll",
1112
@ViewBuilder content: @escaping () -> Content,
1213
eventHandler: SnapToScrollEventHandler? = .none) {
1314

1415
self.content = content
1516
self.alignment = alignment
1617
self.leadingOffset = alignment.scrollOffset
18+
self.spacing = spacing
1719
self.coordinateSpace = coordinateSpace
1820
self.eventHandler = eventHandler
1921
}
@@ -32,6 +34,7 @@ public struct HStackSnap<Content: View>: View {
3234

3335
HStackSnapCore(
3436
leadingOffset: leadingOffset,
37+
spacing: spacing,
3538
coordinateSpace: coordinateSpace,
3639
content: content,
3740
eventHandler: eventHandler)
@@ -50,6 +53,8 @@ public struct HStackSnap<Content: View>: View {
5053
/// Calculated offset based on `SnapLocation`
5154
private let leadingOffset: CGFloat
5255

56+
private let spacing: CGFloat?
57+
5358
private var eventHandler: SnapToScrollEventHandler?
5459

5560
private let coordinateSpace: String

Sources/Views/HStackSnapCore.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ public struct HStackSnapCore<Content: View>: View {
1010

1111
public init(
1212
leadingOffset: CGFloat,
13+
spacing: CGFloat? = nil,
1314
coordinateSpace: String = "SnapToScroll",
1415
@ViewBuilder content: @escaping () -> Content,
1516
eventHandler: SnapToScrollEventHandler? = .none) {
1617

1718
self.content = content
1819
self.targetOffset = leadingOffset
20+
self.spacing = spacing
1921
self.scrollOffset = leadingOffset
2022
self.coordinateSpace = coordinateSpace
2123
self.eventHandler = eventHandler
@@ -27,9 +29,8 @@ public struct HStackSnapCore<Content: View>: View {
2729
GeometryReader { geometry in
2830

2931
HStack {
30-
HStack(content: content)
32+
HStack(spacing: spacing, content: content)
3133
.offset(x: scrollOffset, y: .zero)
32-
.animation(.easeOut(duration: 0.2))
3334

3435
Spacer()
3536
}
@@ -124,7 +125,9 @@ public struct HStackSnapCore<Content: View>: View {
124125
}
125126

126127
// Update state
127-
scrollOffset = closestSnapLocation
128+
withAnimation(.easeOut(duration: 0.2)) {
129+
scrollOffset = closestSnapLocation
130+
}
128131
prevScrollOffset = scrollOffset
129132
}
130133
}
@@ -155,6 +158,9 @@ public struct HStackSnapCore<Content: View>: View {
155158
/// Calculated offset based on `SnapLocation`
156159
@State private var targetOffset: CGFloat
157160

161+
/// Space between content views`
162+
@State private var spacing: CGFloat?
163+
158164
/// The original offset of each frame, used to calculate `scrollOffset`
159165
@State private var snapLocations: [Int: CGFloat] = [:]
160166

0 commit comments

Comments
 (0)