Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertical scrolling support #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ snappingLayout.snapPosition = .right

![right](readmeImages/right.gif)

### Vertical scrolling

With vertical scrolling enabled, `.top`, `.center`, and `.bottom` can snap the cell to their respective parts of the collection view.

```swift
let snappingLayout = SnappingLayout()
snappingLayout.snapPosition = .top
```

![top](readmeImages/top.gif)

## 💬 Contributing
This is an open source project, so feel free to contribute. How?

Expand Down
140 changes: 105 additions & 35 deletions Sources/SnappingLayout/Classes/SnappingLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ public class SnappingLayout: UICollectionViewFlowLayout {
case left
case center
case right
case top
case bottom
}

// MARK: - Properties

/// Position to snap the cells.
public var snapPosition = SnapPositionType.center

/// Minimum horizontal velocity to trigger the snap effect.
/// Minimum velocity to trigger the snap effect.
private let minimumSnapVelocity: CGFloat = 0.3

// MARK: - UICollectionViewFlowLayout
Expand All @@ -29,52 +31,120 @@ public class SnappingLayout: UICollectionViewFlowLayout {
guard let collectionView = collectionView else {
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
}

var offsetAdjusment = CGFloat.greatestFiniteMagnitude
let horizontalPosition: CGFloat

switch snapPosition {
case .left:
horizontalPosition = proposedContentOffset.x + collectionView.contentInset.left + sectionInset.left
case .center:
horizontalPosition = proposedContentOffset.x + (collectionView.bounds.width * 0.5)
case .right:
horizontalPosition = proposedContentOffset.x + collectionView.bounds.width - sectionInset.right
}

let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)
let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect)
layoutAttributesArray?.forEach { layoutAttributes in
let itemHorizontalPosition: CGFloat

let verticalPosition: CGFloat

switch scrollDirection {
case .horizontal:
Comment on lines +38 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of chaining 2 switch statements inside another switch, you can do it like this:

Suggested change
switch scrollDirection {
case .horizontal:
switch (scrollDirection, snapPosition) {
case (.horizontal, .left):
case (.horizontal, .center):
case (.horizontal, .right):
case (.vertical, .center):
case (.vertical, .top):
case (.vertical, .bottom):
case (.horizontal, .top), (.horizontal, .bottom), (.vertical, .left), (.vertical, .right):

switch snapPosition {
case .left:
itemHorizontalPosition = layoutAttributes.frame.minX - collectionView.contentInset.left
horizontalPosition = proposedContentOffset.x + collectionView.contentInset.left + sectionInset.left
verticalPosition = proposedContentOffset.y
case .center:
itemHorizontalPosition = layoutAttributes.center.x
horizontalPosition = proposedContentOffset.x + (collectionView.bounds.width * 0.5)
verticalPosition = proposedContentOffset.y
case .right:
itemHorizontalPosition = layoutAttributes.frame.maxX + collectionView.contentInset.right
horizontalPosition = proposedContentOffset.x + collectionView.bounds.width - sectionInset.right
verticalPosition = proposedContentOffset.y
case .top, .bottom:
horizontalPosition = proposedContentOffset.x
verticalPosition = proposedContentOffset.y
}

if abs(itemHorizontalPosition - horizontalPosition) < abs(offsetAdjusment) {
// If the drag velocity is lower than the minimum velocity (no matter the direction):
// snap the current cell to it's original position.
if abs(velocity.x) < self.minimumSnapVelocity {
offsetAdjusment = itemHorizontalPosition - horizontalPosition
case .vertical:
switch snapPosition {
case .left, .right:
horizontalPosition = proposedContentOffset.x
verticalPosition = proposedContentOffset.y
case .center:
horizontalPosition = proposedContentOffset.x
verticalPosition = proposedContentOffset.y + (collectionView.bounds.height * 0.5)
case .top:
horizontalPosition = proposedContentOffset.x
verticalPosition = proposedContentOffset.y + collectionView.bounds.height - sectionInset.top
case .bottom:
horizontalPosition = proposedContentOffset.x
verticalPosition = proposedContentOffset.y + collectionView.bounds.height - sectionInset.bottom
}
}

let targetRect = CGRect(x: proposedContentOffset.x, y: proposedContentOffset.y, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)
let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect)
layoutAttributesArray?.forEach { layoutAttributes in

switch scrollDirection {
case .horizontal:
let itemHorizontalPosition: CGFloat

switch snapPosition {
case .left:
itemHorizontalPosition = layoutAttributes.frame.minX - collectionView.contentInset.left
case .center:
Comment on lines +75 to +82
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

itemHorizontalPosition = layoutAttributes.center.x
case .right:
itemHorizontalPosition = layoutAttributes.frame.maxX + collectionView.contentInset.right
case .top, .bottom:
itemHorizontalPosition = horizontalPosition
}

if abs(itemHorizontalPosition - horizontalPosition) < abs(offsetAdjusment) {
// If the drag velocity is lower than the minimum velocity (no matter the direction):
// snap the current cell to it's original position.
if abs(velocity.x) < self.minimumSnapVelocity {
offsetAdjusment = itemHorizontalPosition - horizontalPosition
}
// If the velocity is higher than the snap threshold and drag is right->left:
// move to the next cell on the right.
else if velocity.x > 0 {
offsetAdjusment = itemHorizontalPosition - horizontalPosition + (layoutAttributes.bounds.width + self.minimumLineSpacing)
}
// If the velocity is higher than the snap threshold and drag is left->right:
// move to the next cell on the left.
else { // velocity.x < 0
offsetAdjusment = itemHorizontalPosition - horizontalPosition - (layoutAttributes.bounds.width + self.minimumLineSpacing)
}
}
// If the velocity is higher than the snap threshold and drag is right->left:
// move to the next cell on the right.
else if velocity.x > 0 {
offsetAdjusment = itemHorizontalPosition - horizontalPosition + (layoutAttributes.bounds.width + self.minimumLineSpacing)
case .vertical:
let itemVerticalPosition: CGFloat

switch snapPosition {
case .left, .right:
itemVerticalPosition = verticalPosition
case .center:
itemVerticalPosition = layoutAttributes.center.y
case .top:
itemVerticalPosition = layoutAttributes.frame.minY - collectionView.contentInset.top
case .bottom:
itemVerticalPosition = layoutAttributes.frame.minY + collectionView.contentInset.bottom
}
// If the velocity is higher than the snap threshold and drag is left->right:
// move to the next cell on the left.
else { // velocity.x < 0
offsetAdjusment = itemHorizontalPosition - horizontalPosition - (layoutAttributes.bounds.width + self.minimumLineSpacing)

if abs(itemVerticalPosition - verticalPosition) < abs(offsetAdjusment) {
// If the drag velocity is lower than the minimum velocity (no matter the direction):
// snap the current cell to it's original position.
if abs(velocity.y) < self.minimumSnapVelocity {
offsetAdjusment = itemVerticalPosition - verticalPosition
}
// If the velocity is higher than the snap threshold and drag is bottom->top:
// move to the next cell on the bottom.
else if velocity.y > 0 {
offsetAdjusment = itemVerticalPosition - verticalPosition + (layoutAttributes.bounds.height + self.minimumLineSpacing)
}
// If the velocity is higher than the snap threshold and drag is top->bottom:
// move to the next cell on the top.
else { // velocity.x < 0
offsetAdjusment = itemVerticalPosition - verticalPosition - (layoutAttributes.bounds.height + self.minimumLineSpacing)
}
}
}


}

return CGPoint(x: proposedContentOffset.x + offsetAdjusment, y: proposedContentOffset.y)
switch scrollDirection {
case .horizontal:
return CGPoint(x: proposedContentOffset.x + offsetAdjusment, y: proposedContentOffset.y)
case .vertical:
return CGPoint(x: proposedContentOffset.x, y: proposedContentOffset.y + offsetAdjusment)
}
}
}