Skip to content

Commit e3ef131

Browse files
committed
Add spacing support between stack items.
1 parent de0a704 commit e3ef131

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Sources/HStackSnap.swift

Lines changed: 5 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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,7 +29,7 @@ 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)
3234
.animation(.easeOut(duration: 0.2))
3335

@@ -145,6 +147,9 @@ public struct HStackSnapCore<Content: View>: View {
145147
/// Calculated offset based on `SnapLocation`
146148
@State private var targetOffset: CGFloat
147149

150+
/// Space between content views`
151+
@State private var spacing: CGFloat?
152+
148153
/// The original offset of each frame, used to calculate `scrollOffset`
149154
@State private var snapLocations: [Int: CGFloat] = [:]
150155

0 commit comments

Comments
 (0)