Skip to content

Commit d6e8f2f

Browse files
committed
Fixed page jumping when velocity is 0
1 parent 7d8f16a commit d6e8f2f

File tree

7 files changed

+20
-10
lines changed

7 files changed

+20
-10
lines changed

Example/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- MSPeekCollectionViewDelegateImplementation (3.0.1)
2+
- MSPeekCollectionViewDelegateImplementation (3.0.2)
33

44
DEPENDENCIES:
55
- MSPeekCollectionViewDelegateImplementation (from `../`)
@@ -9,7 +9,7 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
MSPeekCollectionViewDelegateImplementation: fdb8ae5c41baaad0962f489b7fa78a653037cc17
12+
MSPeekCollectionViewDelegateImplementation: 0e038d6d665a8721c3732b7c9872b39fca1bf285
1313

1414
PODFILE CHECKSUM: 8d230cee08c8a8b7e62859eff8016277efe2311d
1515

Example/Pods/Local Podspecs/MSPeekCollectionViewDelegateImplementation.podspec.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Manifest.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Pods/Target Support Files/MSPeekCollectionViewDelegateImplementation/MSPeekCollectionViewDelegateImplementation-Info.plist

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MSPeekCollectionViewDelegateImplementation.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'MSPeekCollectionViewDelegateImplementation'
11-
s.version = '3.0.1'
11+
s.version = '3.0.2'
1212
s.summary = 'A custom paging behavior that peeks the previous and next items in a collection view'
1313
s.swift_version = '5.0'
1414

MSPeekCollectionViewDelegateImplementation/Classes/MSCollectionViewPaging.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class MSCollectionViewPaging: NSObject {
6161
let currentIndex = dataSource?.collectionViewPaging(self, indexForItemAtOffset: startingOffset) ?? 0
6262
let targetIndex = dataSource?.collectionViewPaging(self, indexForItemAtOffset: targetOffset) ?? 0
6363

64+
let delta = targetIndex - currentIndex
6465

6566
var offset: Int
6667
switch (currentIndex, targetIndex, abs(velocity)) {
@@ -70,7 +71,7 @@ public class MSCollectionViewPaging: NSObject {
7071

7172
// Otherwise, get the differece between the target and the current indices
7273
default:
73-
offset = abs(targetIndex - currentIndex)
74+
offset = abs(delta)
7475
}
7576

7677
/// If we've set a minimum number of items to scroll, enforce it
@@ -84,7 +85,7 @@ public class MSCollectionViewPaging: NSObject {
8485
}
8586

8687
// The final index is the current index ofsetted by the value and in the velocity direction
87-
var finalIndex = currentIndex + (offset * velocity.sign.multiplier)
88+
var finalIndex = currentIndex + (offset * Sign(value: delta).multiplier)
8889

8990
// Move to index only if it exists. This will solve issues when there are multiple items in the same page
9091
if !(dataSource?.collectionViewPaging(self, indexExists: finalIndex) ?? false) {

MSPeekCollectionViewDelegateImplementation/Classes/Sign.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ enum Sign {
2020
return -1
2121
}
2222
}
23+
24+
init(value: Int) {
25+
if value < 0 {
26+
self = .negative
27+
}
28+
else {
29+
self = .positive
30+
}
31+
}
2332
}
2433

2534
extension CGFloat {

0 commit comments

Comments
 (0)