|
| 1 | +// |
| 2 | +// HorizontalSnappingLayout.swift |
| 3 | +// |
| 4 | + |
| 5 | +/// This works best when setting `collectionView.decelerationRate = .fast`. |
| 6 | +public class SnappingLayout: UICollectionViewFlowLayout { |
| 7 | + |
| 8 | + // MARK: - Enums |
| 9 | + |
| 10 | + public enum SnapPositionType: Int { |
| 11 | + case left |
| 12 | + case center |
| 13 | + case right |
| 14 | + } |
| 15 | + |
| 16 | + // MARK: - Properties |
| 17 | + |
| 18 | + /// Position to snap the cells. |
| 19 | + public var snapPosition = SnapPositionType.center |
| 20 | + |
| 21 | + /// Minimum horizontal velocity to trigger the snap effect. |
| 22 | + private let minimumSnapVelocity: CGFloat = 0.3 |
| 23 | + |
| 24 | + // MARK: - UICollectionViewFlowLayout |
| 25 | + |
| 26 | + public override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint { |
| 27 | + guard let collectionView = collectionView else { |
| 28 | + return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) |
| 29 | + } |
| 30 | + |
| 31 | + var offsetAdjusment = CGFloat.greatestFiniteMagnitude |
| 32 | + let horizontalPosition: CGFloat |
| 33 | + |
| 34 | + switch snapPosition { |
| 35 | + case .left: |
| 36 | + horizontalPosition = proposedContentOffset.x + collectionView.contentInset.left + sectionInset.left |
| 37 | + case .center: |
| 38 | + horizontalPosition = proposedContentOffset.x + (collectionView.bounds.width * 0.5) |
| 39 | + case .right: |
| 40 | + horizontalPosition = proposedContentOffset.x + collectionView.bounds.width - sectionInset.right |
| 41 | + } |
| 42 | + |
| 43 | + let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height) |
| 44 | + let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect) |
| 45 | + layoutAttributesArray?.forEach { layoutAttributes in |
| 46 | + let itemHorizontalPosition: CGFloat |
| 47 | + |
| 48 | + switch snapPosition { |
| 49 | + case .left: |
| 50 | + itemHorizontalPosition = layoutAttributes.frame.minX - collectionView.contentInset.left |
| 51 | + case .center: |
| 52 | + itemHorizontalPosition = layoutAttributes.center.x |
| 53 | + case .right: |
| 54 | + itemHorizontalPosition = layoutAttributes.frame.maxX + collectionView.contentInset.right |
| 55 | + } |
| 56 | + |
| 57 | + if abs(itemHorizontalPosition - horizontalPosition) < abs(offsetAdjusment) { |
| 58 | + // If the drag velocity is lower than the minimum velocity (no matter the direction): |
| 59 | + // snap the current cell to it's original position. |
| 60 | + if abs(velocity.x) < self.minimumSnapVelocity { |
| 61 | + offsetAdjusment = itemHorizontalPosition - horizontalPosition |
| 62 | + } |
| 63 | + // If the velocity is higher than the snap threshold and drag is right->left: |
| 64 | + // move to the next cell on the right. |
| 65 | + else if velocity.x > 0 { |
| 66 | + offsetAdjusment = itemHorizontalPosition - horizontalPosition + (layoutAttributes.bounds.width + self.minimumLineSpacing) |
| 67 | + } |
| 68 | + // If the velocity is higher than the snap threshold and drag is left->right: |
| 69 | + // move to the next cell on the left. |
| 70 | + else { // velocity.x < 0 |
| 71 | + offsetAdjusment = itemHorizontalPosition - horizontalPosition - (layoutAttributes.bounds.width + self.minimumLineSpacing) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return CGPoint(x: proposedContentOffset.x + offsetAdjusment, y: proposedContentOffset.y) |
| 77 | + } |
| 78 | +} |
0 commit comments