From 1ce9f0b303c3fd47c988d9bf6935d794be3066e9 Mon Sep 17 00:00:00 2001 From: tomasandrle <42672549+tomasandrle@users.noreply.github.com> Date: Mon, 22 Mar 2021 14:38:32 +0100 Subject: [PATCH] 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 --- .../MSCollectionViewCellPeekingLayout.swift | 9 +++++---- .../MSCollectionViewPeekingBehavior.swift | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewCellPeekingLayout.swift b/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewCellPeekingLayout.swift index 7289eef..9e8208f 100644 --- a/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewCellPeekingLayout.swift +++ b/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewCellPeekingLayout.swift @@ -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 } @@ -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) diff --git a/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift b/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift index 5388242..ac994b5 100644 --- a/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift +++ b/Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift @@ -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: