Skip to content

Commit 6adddf3

Browse files
author
Trent Guillory
committed
Add comments.
1 parent 81aa5a3 commit 6adddf3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Sources/HStackSnap.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public struct HStackSnap<Content: View>: View {
111111
let currOffset = scrollOffset
112112
var closestSnapLocation: CGFloat = snapLocations.first?.value ?? targetOffset
113113

114+
// Calculate closest snap location
114115
for (_, offset) in snapLocations {
115116

116117
if abs(offset - currOffset) < abs(closestSnapLocation - currOffset) {
@@ -119,15 +120,22 @@ public struct HStackSnap<Content: View>: View {
119120
}
120121
}
121122

123+
// Handle swipe callback
122124
let selectedIndex = snapLocations.map { $0.value }.sorted(by: { $0 > $1 })
123125
.firstIndex(of: closestSnapLocation) ?? 0
124126

125-
swipeEventHandler?(selectedIndex)
127+
if selectedIndex != previouslySentIndex {
128+
129+
swipeEventHandler?(selectedIndex)
130+
previouslySentIndex = selectedIndex
131+
}
132+
133+
// Update state
126134
scrollOffset = closestSnapLocation
127135
prevScrollOffset = scrollOffset
128136
}
129137
}
130-
138+
131139
func scrollOffset(for x: CGFloat) -> CGFloat {
132140

133141
return (targetOffset * 2) - x
@@ -151,5 +159,7 @@ public struct HStackSnap<Content: View>: View {
151159

152160
private var swipeEventHandler: SwipeEventHandler?
153161

162+
@State private var previouslySentIndex: Int = 0
163+
154164
private let coordinateSpace: String
155165
}

0 commit comments

Comments
 (0)