Skip to content

Commit

Permalink
Added a way to get the current index on paging
Browse files Browse the repository at this point in the history
  • Loading branch information
MaherKSantina committed May 8, 2020
1 parent dbfeb73 commit 58466f7
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ extension ViewController: UICollectionViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print(behavior.currentIndex)
}
}
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- MSPeekCollectionViewDelegateImplementation (3.1.0)
- MSPeekCollectionViewDelegateImplementation (3.1.1)

DEPENDENCIES:
- MSPeekCollectionViewDelegateImplementation (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
MSPeekCollectionViewDelegateImplementation: 7115b89c6065613e77045836730eb2f63c3bc958
MSPeekCollectionViewDelegateImplementation: f86fd6a6ce284723343e8697a013f5294c05e745

PODFILE CHECKSUM: 8d230cee08c8a8b7e62859eff8016277efe2311d

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

282 changes: 139 additions & 143 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion MSPeekCollectionViewDelegateImplementation.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'MSPeekCollectionViewDelegateImplementation'
s.version = '3.1.0'
s.version = '3.1.1'
s.summary = 'A custom paging behavior that peeks the previous and next items in a collection view'
s.swift_version = '5.0'

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ extension ViewController: UICollectionViewDelegate {
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
behavior.scrollViewWillEndDragging(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print(behavior.currentIndex)
}
}
```

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

### Listen to index changes
You can use the scroll view's delegate function to do that (Make sure you conform to `UICollectionViewDelegate`):
```
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print(behavior.currentIndex)
}
```

## Customization
### Vertical Scroll Direction
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class MSCollectionViewPaging: NSObject {

var currentContentOffset: CGFloat = 0

public var currentIndex: Int {
return dataSource?.collectionViewPaging(self, indexForItemAtOffset: currentContentOffset) ?? 0
}

var velocityThreshold: CGFloat {
return dataSource?.collectionViewPagingVelocityThreshold(self) ?? 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class MSCollectionViewPeekingBehavior {
return layout.collectionView?.numberOfItems(inSection: 0) ?? 0
}

/// Returns the current index of the left most item
public var currentIndex: Int {
return paging.currentIndex
}

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) {
self.cellSpacing = cellSpacing
self.cellPeekWidth = cellPeekWidth
Expand Down

This file was deleted.

0 comments on commit 58466f7

Please sign in to comment.