Skip to content

Add spacing support between stack items. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Sources/HStackSnap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ public struct HStackSnap<Content: View>: View {

public init(
alignment: SnapAlignment,
spacing: CGFloat? = nil,
coordinateSpace: String = "SnapToScroll",
@ViewBuilder content: @escaping () -> Content,
eventHandler: SnapToScrollEventHandler? = .none) {

self.content = content
self.alignment = alignment
self.leadingOffset = alignment.scrollOffset
self.spacing = spacing
self.coordinateSpace = coordinateSpace
self.eventHandler = eventHandler
}
Expand All @@ -32,6 +34,7 @@ public struct HStackSnap<Content: View>: View {

HStackSnapCore(
leadingOffset: leadingOffset,
spacing: spacing,
coordinateSpace: coordinateSpace,
content: content,
eventHandler: eventHandler)
Expand All @@ -50,6 +53,8 @@ public struct HStackSnap<Content: View>: View {
/// Calculated offset based on `SnapLocation`
private let leadingOffset: CGFloat

private let spacing: CGFloat?

private var eventHandler: SnapToScrollEventHandler?

private let coordinateSpace: String
Expand Down
12 changes: 9 additions & 3 deletions Sources/Views/HStackSnapCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public struct HStackSnapCore<Content: View>: View {

public init(
leadingOffset: CGFloat,
spacing: CGFloat? = nil,
coordinateSpace: String = "SnapToScroll",
@ViewBuilder content: @escaping () -> Content,
eventHandler: SnapToScrollEventHandler? = .none) {

self.content = content
self.targetOffset = leadingOffset
self.spacing = spacing
self.scrollOffset = leadingOffset
self.coordinateSpace = coordinateSpace
self.eventHandler = eventHandler
Expand All @@ -27,9 +29,8 @@ public struct HStackSnapCore<Content: View>: View {
GeometryReader { geometry in

HStack {
HStack(content: content)
HStack(spacing: spacing, content: content)
.offset(x: scrollOffset, y: .zero)
.animation(.easeOut(duration: 0.2))

Spacer()
}
Expand Down Expand Up @@ -123,7 +124,9 @@ public struct HStackSnapCore<Content: View>: View {
}

// Update state
scrollOffset = closestSnapLocation
withAnimation(.easeOut(duration: 0.2)) {
scrollOffset = closestSnapLocation
}
prevScrollOffset = scrollOffset
}
}
Expand All @@ -145,6 +148,9 @@ public struct HStackSnapCore<Content: View>: View {
/// Calculated offset based on `SnapLocation`
@State private var targetOffset: CGFloat

/// Space between content views`
@State private var spacing: CGFloat?

/// The original offset of each frame, used to calculate `scrollOffset`
@State private var snapLocations: [Int: CGFloat] = [:]

Expand Down