-
Notifications
You must be signed in to change notification settings - Fork 5
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
Lcarvajal-zz
wants to merge
7
commits into
getmimo:master
Choose a base branch
from
Lcarvajal-zz:feature/verticalScrollingSupport
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−35
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6aac568
Add support for .top snapping for vertical scrolling
lcarvajal e2841e5
Add support for center snap in vertical scrolling
lcarvajal ef4d878
Add support for bottom snapping on vertical scroll
lcarvajal 20f0eb9
Add section on vertical scrolling to README
lcarvajal 93ade53
Update comments describing scrolling logic
lcarvajal ba542b3
Add switch statement to return statement
lcarvajal 16b9cd3
Remove horizontal from comment on scroll velocity
lcarvajal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 anotherswitch
, you can do it like this: