Skip to content

Commit 9972d98

Browse files
committed
Remove force-unwraps and improve comments
1 parent 6820a9a commit 9972d98

1 file changed

Lines changed: 33 additions & 21 deletions

File tree

Classes/GLCollectionTableViewCell.swift

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,51 @@ class GLIndexedCollectionViewFlowLayout: UICollectionViewFlowLayout {
3737
}
3838

3939
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
40+
// If the UICollectionView has paginatedScroll set to false there is no
41+
// need to apply any pagination logic, so we simply return the current
42+
// proposedContentOffset coordinates.
4043
guard paginatedScroll == true else {
4144
return CGPoint(x: proposedContentOffset.x, y: 0)
4245
}
4346

47+
// It's not a bad idea to shield us for some strange cases where the
48+
// UICollectionView won't be there for any reason, since it comes in an
49+
// Optional flavor. If the UICollectionView won't be available we return
50+
// the current proposedContentOffset coordinates and exit.
51+
guard let collectionView: UICollectionView = collectionView else {
52+
return CGPoint(x: proposedContentOffset.x, y: 0)
53+
}
54+
55+
let scannerFrame: CGRect = CGRect(x: proposedContentOffset.x,
56+
y: 0,
57+
width: collectionView.bounds.width,
58+
height: collectionView.bounds.height)
59+
60+
// If there is no UICollectionViewLayoutAttributes for the given
61+
// scannerFrame there is no reason to calculate a paginated layout for
62+
// it, so we the current proposedContentOffset coordinates.
63+
guard let layoutAttributes: [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: scannerFrame) else {
64+
return CGPoint(x: proposedContentOffset.x, y: 0)
65+
}
66+
4467
// To make paginated scrolling work fine this CGFloat below MUST be
4568
// equal to the value set in the insetForSectionAt method of
4669
// UICollectionView's UICollectionViewDelegate Flow Layout.
4770
let collectionViewInsets: CGFloat = 10.0
4871

49-
// Since UICollectionViewFlowLayout proposedContentOffset coordinates
72+
// Since UICollectionViewFlowLayout's proposedContentOffset coordinates
5073
// won't take count of any UICollectionView UIEdgeInsets values we need
5174
// to fix it by adding collectionViewInsets to the .x coordinate.
5275
//
53-
// Note: This will only cover horizontal scrolling and pagination, if you
54-
// need vertical pagination replace the .x coordinate with .y and update
55-
// collectionViewInsets value with the appropriate one.
76+
// Note: This will only cover horizontal scrolling and pagination, if
77+
// you need vertical pagination replace the .x coordinate with .y and
78+
// update collectionViewInsets value with the appropriate one.
5679
let proposedXCoordWithInsets: CGFloat = proposedContentOffset.x + collectionViewInsets
5780

5881
// We now create a variable and we assign a very high CGFloat to it (a
59-
// big number here is needed to cover very large
60-
// UICollectionViewContentSize cases). This var will hold the needed
61-
// horizontal adjustment to make the UICollectionView paginate scroll.
82+
// to cover very large UICollectionViewContentSize cases).
83+
// This var will hold the needed horizontal adjustment to make the
84+
// UICollectionView paginate scroll.
6285
var offsetCorrection: CGFloat = .greatestFiniteMagnitude
6386

6487
// Now we loop through all the different layout attributes of the
@@ -67,22 +90,11 @@ class GLIndexedCollectionViewFlowLayout: UICollectionViewFlowLayout {
6790
// cell which needs the least offsetCorrection value: it will mean that
6891
// it's the first cell on the left of the screen which will give
6992
// pagination.
70-
for layoutAttribute: UICollectionViewLayoutAttributes in super.layoutAttributesForElements(in: CGRect(x: proposedContentOffset.x, y: 0, width: collectionView!.bounds.width, height: collectionView!.bounds.height))! {
71-
// layoutAttributesForElements may contain all sort of layout
93+
layoutAttributes.forEach { (layoutAttribute) in
94+
// UICollectionViewLayoutAttributes may contain all sort of layout
7295
// attributes so we need to check if it belongs to a
7396
// UICollectionViewCell, otherwise logic won't work.
7497
if layoutAttribute.representedElementCategory == .cell {
75-
// Since UICollectionViewFlowLayout's UICollectionView comes in
76-
// an Optional flavour and we will need to get it's frame size
77-
// down below, it's not a bad idea to shield us from those very
78-
// strange cases in which the UICollectionView won't be there
79-
// (for whatever reason) and exit the loop since it is useless
80-
// to calculate an offsetCorrection for a non-existent
81-
// UICollectionView.
82-
guard let collectionView: UICollectionView = collectionView else {
83-
break
84-
}
85-
8698
// To accurately calculate the offsetCorrection we will check
8799
// only the cells contained in one half of UICollectionView's
88100
// width, following the scrolling direction. The check will be
@@ -91,7 +103,7 @@ class GLIndexedCollectionViewFlowLayout: UICollectionViewFlowLayout {
91103
let discardableScrollingElementsFrame: CGFloat = collectionView.contentOffset.x + (collectionView.frame.size.width / 2)
92104

93105
if (layoutAttribute.center.x <= discardableScrollingElementsFrame && velocity.x > 0) || (layoutAttribute.center.x >= discardableScrollingElementsFrame && velocity.x < 0) {
94-
continue
106+
return
95107
}
96108

97109
if abs(layoutAttribute.frame.origin.x - proposedXCoordWithInsets) < abs(offsetCorrection) {

0 commit comments

Comments
 (0)