Skip to content

Commit 5940b23

Browse files
Oliver MastnyMaherKSantina
authored andcommitted
Add minimumItemsToScroll functionality (#46)
* Add minimumItemsToScroll functionality * Added minimumItemsToScroll, bumped version number to 1.3.0
1 parent e9990a4 commit 5940b23

File tree

9 files changed

+36
-9
lines changed

9 files changed

+36
-9
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 (1.2.2)
2+
- MSPeekCollectionViewDelegateImplementation (1.3.0)
33

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

1111
SPEC CHECKSUMS:
12-
MSPeekCollectionViewDelegateImplementation: dc9c3aa660f03c4b2bb84edb1144c87ba562915e
12+
MSPeekCollectionViewDelegateImplementation: 09fdbcd804848033804ee66ce93ac9a0ebba8262
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.

Example/Tests/HorizontalScrollDirectionTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ class HorizontalScrollDirectionTests: XCTestCase {
144144
XCTAssertEqual(simulatedTargetContentOffset.pointee.x, 520)
145145
}
146146

147+
func test_MinimumItemsToScroll_ShouldScrollProperly() {
148+
collectionView.frame = CGRect(x: 0, y: 0, width: 320, height: 200)
149+
sut = MSPeekCollectionViewDelegateImplementation(cellSpacing: 20, cellPeekWidth: 20, scrollThreshold: 50, minimumItemsToScroll: 2, maximumItemsToScroll: 4)
150+
let simulatedTargetContentOffset = simulateHorizontalScroll(distance: 51)
151+
XCTAssertEqual(simulatedTargetContentOffset.pointee.x, 520)
152+
}
153+
147154
func test_contentOffsetAtIndex_ShouldReturnCorrectOffset() {
148155
collectionView.frame = CGRect(x: 0, y: 0, width: 320, height: 200)
149156
sut = MSPeekCollectionViewDelegateImplementation(cellSpacing: 20, cellPeekWidth: 20, scrollThreshold: 50, maximumItemsToScroll: 2)

Example/Tests/VerticalScrollDirectionTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ class VerticalScrollDirectionTests: XCTestCase {
126126
XCTAssertEqual(simulatedTargetContentOffset.pointee.y, 280)
127127
}
128128

129+
130+
func test_MinimumItemsToScroll_ShouldScrollProperly() {
131+
collectionView.frame = CGRect(x: 0, y: 0, width: 320, height: 200)
132+
sut = MSPeekCollectionViewDelegateImplementation(cellSpacing: 20, cellPeekWidth: 20, scrollThreshold: 50, minimumItemsToScroll: 2, maximumItemsToScroll: 4, scrollDirection: .vertical)
133+
let simulatedTargetContentOffset = simulateVerticalScroll(distance: 51)
134+
XCTAssertEqual(simulatedTargetContentOffset.pointee.y, 280)
135+
}
136+
129137
func test_contentOffsetAtIndex_ShouldReturnCorrectOffset() {
130138
collectionView.frame = CGRect(x: 0, y: 0, width: 320, height: 200)
131139
sut = MSPeekCollectionViewDelegateImplementation(cellSpacing: 20, cellPeekWidth: 20, scrollThreshold: 50, maximumItemsToScroll: 2, scrollDirection: .vertical)

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 = '1.2.2'
11+
s.version = '1.3.0'
1212
s.summary = 'A custom paging behavior that peeks the previous and next items in a collection view'
1313
s.swift_version = '4.2'
1414

MSPeekCollectionViewDelegateImplementation/Classes/MSPeekCollectionViewDelegateImplementation.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ open class MSPeekCollectionViewDelegateImplementation: NSObject {
3333
public let cellPeekWidth: CGFloat
3434
public let cellSpacing: CGFloat
3535
public let scrollThreshold: CGFloat
36+
public let minimumItemsToScroll: Int
3637
public let maximumItemsToScroll: Int
3738
public let numberOfItemsToShow: Int
3839
public let scrollDirection: UICollectionView.ScrollDirection
@@ -55,10 +56,11 @@ open class MSPeekCollectionViewDelegateImplementation: NSObject {
5556
return max(0, finalWidth)
5657
}
5758

58-
public init(cellSpacing: CGFloat = 20, cellPeekWidth: CGFloat = 20, scrollThreshold: CGFloat = 50, maximumItemsToScroll: Int = 1, numberOfItemsToShow: Int = 1, scrollDirection: UICollectionView.ScrollDirection = .horizontal) {
59+
public init(cellSpacing: CGFloat = 20, cellPeekWidth: CGFloat = 20, scrollThreshold: CGFloat = 50, minimumItemsToScroll: Int = 1, maximumItemsToScroll: Int = 1, numberOfItemsToShow: Int = 1, scrollDirection: UICollectionView.ScrollDirection = .horizontal) {
5960
self.cellSpacing = cellSpacing
6061
self.cellPeekWidth = cellPeekWidth
6162
self.scrollThreshold = scrollThreshold
63+
self.minimumItemsToScroll = minimumItemsToScroll
6264
self.maximumItemsToScroll = maximumItemsToScroll
6365
self.numberOfItemsToShow = numberOfItemsToShow
6466
self.scrollDirection = scrollDirection
@@ -91,6 +93,12 @@ open class MSPeekCollectionViewDelegateImplementation: NSObject {
9193
coefficent = Int(scrollDistance/scrollWidth)
9294
}
9395

96+
if coefficent > 0 {
97+
coefficent = max(minimumItemsToScroll, coefficent)
98+
} else if coefficent < 0 {
99+
coefficent = min(-minimumItemsToScroll, coefficent)
100+
}
101+
94102
let finalCoefficent = max((-1) * maximumItemsToScroll, min(coefficent, maximumItemsToScroll))
95103
return finalCoefficent
96104
}

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ delegate = MSPeekCollectionViewDelegateImplementation(cellPeekWidth: 20)
7272
delegate = MSPeekCollectionViewDelegateImplementation(scrollThreshold: 150)
7373
```
7474
```swift
75+
//minimumItemsToScroll is the minimum number of items that can be scrolled
76+
delegate = MSPeekCollectionViewDelegateImplementation(minimumItemsToScroll: 1)
77+
```
78+
```swift
7579
//maximumItemsToScroll is the maximum number of items that can be scrolled if the scroll distance is large
7680
delegate = MSPeekCollectionViewDelegateImplementation(maximumItemsToScroll: 3)
7781
```

0 commit comments

Comments
 (0)