Skip to content

Commit 1ce9f0b

Browse files
authored
Better handling of negative cellSpacing (#71)
* Better handling of negative cellSpacing Avoid glitching when cellSpacing is set to a negative value. * Rename parameter to allowNegativeValues * Renamed parameter to allowNegativeValues
1 parent 91fea6d commit 1ce9f0b

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewCellPeekingLayout.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ open class MSCollectionViewCellPeekingLayout: UICollectionViewLayout {
5656
override open var collectionViewContentSize: CGSize {
5757
switch scrollDirection {
5858
case .horizontal:
59-
return CGSize(width: contentLength(axis: .main), height: contentLength(axis: .cross))
59+
return CGSize(width: contentLength(axis: .main, allowNegativeValues: false), height: contentLength(axis: .cross, allowNegativeValues: false))
6060
case .vertical:
61-
return CGSize(width: contentLength(axis: .cross), height: contentLength(axis: .main))
61+
return CGSize(width: contentLength(axis: .cross, allowNegativeValues: false), height: contentLength(axis: .main, allowNegativeValues: false))
6262
default:
6363
return .zero
6464
}
@@ -98,11 +98,12 @@ open class MSCollectionViewCellPeekingLayout: UICollectionViewLayout {
9898
}
9999
}
100100

101-
func contentLength(axis: Axis) -> CGFloat {
101+
func contentLength(axis: Axis, allowNegativeValues: Bool) -> CGFloat {
102+
let spacing = allowNegativeValues ? spacingLength * 2 : max(0, spacingLength * 2)
102103
switch axis {
103104
case .main:
104105
let length = itemLength(axis: .main)
105-
let offsets = spacingLength * 2 + peekingLength * 2 //One from the start and one at the end
106+
let offsets = spacing + peekingLength * 2 // One from the start and one at the end
106107
return (length * CGFloat(numberOfItems)) + (CGFloat(numberOfItems) * spacingLength) + offsets
107108
case .cross:
108109
return itemLength(axis: .cross)

Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension MSCollectionViewPeekingBehavior: MSCollectionViewPagingDataSource {
111111
}
112112

113113
public func collectionViewPaging(_ collectionViewPaging: MSCollectionViewPaging, indexForItemAtOffset offset: CGFloat) -> Int {
114-
let safeOffset = min(max(0, offset), layout.contentLength(axis: .main))
114+
let safeOffset = min(max(0, offset), layout.contentLength(axis: .main, allowNegativeValues: true))
115115
let point: CGPoint
116116
switch (scrollDirection) {
117117
case .horizontal:

0 commit comments

Comments
 (0)