Skip to content

Commit 58466f7

Browse files
committed
Added a way to get the current index on paging
1 parent dbfeb73 commit 58466f7

File tree

11 files changed

+172
-223
lines changed

11 files changed

+172
-223
lines changed

Example/MSPeekCollectionViewDelegateImplementation/ViewController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,8 @@ extension ViewController: UICollectionViewDelegate {
9292
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
9393
behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
9494
}
95+
96+
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
97+
print(behavior.currentIndex)
98+
}
9599
}

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.1.0)
2+
- MSPeekCollectionViewDelegateImplementation (3.1.1)
33

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

1111
SPEC CHECKSUMS:
12-
MSPeekCollectionViewDelegateImplementation: 7115b89c6065613e77045836730eb2f63c3bc958
12+
MSPeekCollectionViewDelegateImplementation: f86fd6a6ce284723343e8697a013f5294c05e745
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/Pods.xcodeproj/project.pbxproj

Lines changed: 139 additions & 143 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.1.0'
11+
s.version = '3.1.1'
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

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ extension ViewController: UICollectionViewDelegate {
182182
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
183183
behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
184184
}
185+
186+
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
187+
print(behavior.currentIndex)
188+
}
185189
}
186190
```
187191

@@ -197,6 +201,14 @@ You can do something like:
197201
behavior.scrollToItem(at: 1, animated: true)
198202
```
199203

204+
### Listen to index changes
205+
You can use the scroll view's delegate function to do that (Make sure you conform to `UICollectionViewDelegate`):
206+
```
207+
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
208+
print(behavior.currentIndex)
209+
}
210+
```
211+
200212
## Customization
201213
### Vertical Scroll Direction
202214
The implementation supports collection views with vertical directions and will automatically position cells correctly, you can set the scrolling and peeking to be vertical using:

Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPaging.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class MSCollectionViewPaging: NSObject {
3636

3737
var currentContentOffset: CGFloat = 0
3838

39+
public var currentIndex: Int {
40+
return dataSource?.collectionViewPaging(self, indexForItemAtOffset: currentContentOffset) ?? 0
41+
}
42+
3943
var velocityThreshold: CGFloat {
4044
return dataSource?.collectionViewPagingVelocityThreshold(self) ?? 0
4145
}

Sources/MSPeekCollectionViewDelegateImplementation/MSCollectionViewPeekingBehavior.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public class MSCollectionViewPeekingBehavior {
5353
return layout.collectionView?.numberOfItems(inSection: 0) ?? 0
5454
}
5555

56+
/// Returns the current index of the left most item
57+
public var currentIndex: Int {
58+
return paging.currentIndex
59+
}
60+
5661
public init(cellSpacing: CGFloat = 20, cellPeekWidth: CGFloat = 20, minimumItemsToScroll: Int? = nil, maximumItemsToScroll: Int? = nil, numberOfItemsToShow: Int = 1, scrollDirection: UICollectionView.ScrollDirection = .horizontal, velocityThreshold: CGFloat = 0.2) {
5762
self.cellSpacing = cellSpacing
5863
self.cellPeekWidth = cellPeekWidth

0 commit comments

Comments
 (0)