Skip to content

Commit

Permalink
Better handling of negative cellSpacing (#71)
Browse files Browse the repository at this point in the history
* Better handling of negative cellSpacing

Avoid glitching when cellSpacing is set to a negative value.

* Rename parameter to allowNegativeValues

* Renamed parameter to allowNegativeValues
  • Loading branch information
tomasandrle authored Mar 22, 2021
1 parent 91fea6d commit 1ce9f0b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ open class MSCollectionViewCellPeekingLayout: UICollectionViewLayout {
override open var collectionViewContentSize: CGSize {
switch scrollDirection {
case .horizontal:
return CGSize(width: contentLength(axis: .main), height: contentLength(axis: .cross))
return CGSize(width: contentLength(axis: .main, allowNegativeValues: false), height: contentLength(axis: .cross, allowNegativeValues: false))
case .vertical:
return CGSize(width: contentLength(axis: .cross), height: contentLength(axis: .main))
return CGSize(width: contentLength(axis: .cross, allowNegativeValues: false), height: contentLength(axis: .main, allowNegativeValues: false))
default:
return .zero
}
Expand Down Expand Up @@ -98,11 +98,12 @@ open class MSCollectionViewCellPeekingLayout: UICollectionViewLayout {
}
}

func contentLength(axis: Axis) -> CGFloat {
func contentLength(axis: Axis, allowNegativeValues: Bool) -> CGFloat {
let spacing = allowNegativeValues ? spacingLength * 2 : max(0, spacingLength * 2)
switch axis {
case .main:
let length = itemLength(axis: .main)
let offsets = spacingLength * 2 + peekingLength * 2 //One from the start and one at the end
let offsets = spacing + peekingLength * 2 // One from the start and one at the end
return (length * CGFloat(numberOfItems)) + (CGFloat(numberOfItems) * spacingLength) + offsets
case .cross:
return itemLength(axis: .cross)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ extension MSCollectionViewPeekingBehavior: MSCollectionViewPagingDataSource {
}

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

0 comments on commit 1ce9f0b

Please sign in to comment.