2121typedef struct ASDirectionalScreenfulBuffer ASDirectionalScreenfulBuffer;
2222
2323ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferHorizontal (ASScrollDirection scrollDirection,
24- ASRangeTuningParameters rangeTuningParameters) {
24+ ASRangeTuningParameters rangeTuningParameters)
25+ {
2526 ASDirectionalScreenfulBuffer horizontalBuffer = {0 , 0 };
2627 BOOL movingRight = ASScrollDirectionContainsRight (scrollDirection);
2728 horizontalBuffer.positiveDirection = movingRight ? rangeTuningParameters.leadingBufferScreenfuls :
@@ -32,7 +33,8 @@ ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferHorizontal(ASScrollDire
3233}
3334
3435ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferVertical (ASScrollDirection scrollDirection,
35- ASRangeTuningParameters rangeTuningParameters) {
36+ ASRangeTuningParameters rangeTuningParameters)
37+ {
3638 ASDirectionalScreenfulBuffer verticalBuffer = {0 , 0 };
3739 BOOL movingDown = ASScrollDirectionContainsDown (scrollDirection);
3840 verticalBuffer.positiveDirection = movingDown ? rangeTuningParameters.leadingBufferScreenfuls :
@@ -52,19 +54,26 @@ ASDirectionalScreenfulBuffer ASDirectionalScreenfulBufferVertical(ASScrollDirect
5254#pragma mark -
5355#pragma mark ASCollectionViewLayoutController
5456
55- @interface ASCollectionViewLayoutController () {
56- ASCollectionView * __weak _collectionView;
57+ @interface ASCollectionViewLayoutController ()
58+ {
59+ UIScrollView * __weak _scrollView;
60+ UICollectionViewLayout * __strong _collectionViewLayout;
5761 std::vector<CGRect> _updateRangeBoundsIndexedByRangeType;
62+ ASScrollDirection _scrollableDirections;
5863}
5964@end
6065
6166@implementation ASCollectionViewLayoutController
6267
63- - (instancetype )initWithCollectionView : (ASCollectionView *)collectionView {
68+ - (instancetype )initWithCollectionView : (ASCollectionView *)collectionView
69+ {
6470 if (!(self = [super init ])) {
6571 return nil ;
6672 }
67- _collectionView = collectionView;
73+
74+ _scrollableDirections = [collectionView scrollableDirections ];
75+ _scrollView = collectionView;
76+ _collectionViewLayout = [collectionView collectionViewLayout ];
6877 _updateRangeBoundsIndexedByRangeType = std::vector<CGRect>(ASLayoutRangeTypeCount);
6978 return self;
7079}
@@ -74,22 +83,21 @@ - (instancetype)initWithCollectionView:(ASCollectionView *)collectionView {
7483
7584- (NSSet *)indexPathsForScrolling : (ASScrollDirection)scrollDirection
7685 viewportSize : (CGSize)viewportSize
77- rangeType : (ASLayoutRangeType)rangeType {
86+ rangeType : (ASLayoutRangeType)rangeType
87+ {
7888 ASRangeGeometry rangeGeometry = [self rangeGeometryWithScrollDirection: scrollDirection
79- collectionView: _collectionView
8089 rangeTuningParameters: [self tuningParametersForRangeType: rangeType]];
8190 _updateRangeBoundsIndexedByRangeType[rangeType] = rangeGeometry.updateBounds ;
82- return [self indexPathsForItemsWithinRangeBounds: rangeGeometry.rangeBounds collectionView: _collectionView ];
91+ return [self indexPathsForItemsWithinRangeBounds: rangeGeometry.rangeBounds];
8392}
8493
8594- (ASRangeGeometry)rangeGeometryWithScrollDirection : (ASScrollDirection)scrollDirection
86- collectionView : (ASCollectionView *)collectionView
87- rangeTuningParameters : (ASRangeTuningParameters)rangeTuningParameters {
88- CGRect rangeBounds = collectionView.bounds ;
89- CGRect updateBounds = collectionView.bounds ;
90- ASScrollDirection scrollableDirections = [collectionView scrollableDirections ];
95+ rangeTuningParameters : (ASRangeTuningParameters)rangeTuningParameters
96+ {
97+ CGRect rangeBounds = _scrollView.bounds ;
98+ CGRect updateBounds = _scrollView.bounds ;
9199
92- BOOL canScrollHorizontally = ASScrollDirectionContainsHorizontalDirection (scrollableDirections );
100+ BOOL canScrollHorizontally = ASScrollDirectionContainsHorizontalDirection (_scrollableDirections );
93101 if (canScrollHorizontally) {
94102 ASDirectionalScreenfulBuffer horizontalBuffer = ASDirectionalScreenfulBufferHorizontal (scrollDirection,
95103 rangeTuningParameters);
@@ -102,7 +110,7 @@ - (ASRangeGeometry)rangeGeometryWithScrollDirection:(ASScrollDirection)scrollDir
102110 MIN (horizontalBuffer.positiveDirection * 0.5 , 0.95 ));
103111 }
104112
105- BOOL canScrollVertically = ASScrollDirectionContainsVerticalDirection (scrollableDirections );
113+ BOOL canScrollVertically = ASScrollDirectionContainsVerticalDirection (_scrollableDirections );
106114 if (canScrollVertically) {
107115 ASDirectionalScreenfulBuffer verticalBuffer = ASDirectionalScreenfulBufferVertical (scrollDirection,
108116 rangeTuningParameters);
@@ -118,9 +126,10 @@ - (ASRangeGeometry)rangeGeometryWithScrollDirection:(ASScrollDirection)scrollDir
118126 return {rangeBounds, updateBounds};
119127}
120128
121- - (NSSet *)indexPathsForItemsWithinRangeBounds : (CGRect)rangeBounds collectionView : (ASCollectionView *)collectionView {
129+ - (NSSet *)indexPathsForItemsWithinRangeBounds : (CGRect)rangeBounds
130+ {
122131 NSMutableSet *indexPathSet = [[NSMutableSet alloc ] init ];
123- NSArray *layoutAttributes = [collectionView.collectionViewLayout layoutAttributesForElementsInRect: rangeBounds];
132+ NSArray *layoutAttributes = [_collectionViewLayout layoutAttributesForElementsInRect: rangeBounds];
124133 for (UICollectionViewLayoutAttributes *la in layoutAttributes) {
125134 [indexPathSet addObject: la.indexPath];
126135 }
@@ -132,13 +141,14 @@ - (NSSet *)indexPathsForItemsWithinRangeBounds:(CGRect)rangeBounds collectionVie
132141
133142- (BOOL )shouldUpdateForVisibleIndexPaths : (NSArray *)indexPaths
134143 viewportSize : (CGSize)viewportSize
135- rangeType : (ASLayoutRangeType)rangeType {
144+ rangeType : (ASLayoutRangeType)rangeType
145+ {
136146 CGRect updateRangeBounds = _updateRangeBoundsIndexedByRangeType[rangeType];
137147 if (CGRectIsEmpty (updateRangeBounds)) {
138148 return YES ;
139149 }
140150
141- CGRect currentBounds = _collectionView .bounds ;
151+ CGRect currentBounds = _scrollView .bounds ;
142152 if (CGRectIsEmpty (currentBounds)) {
143153 currentBounds = CGRectMake (0 , 0 , viewportSize.width , viewportSize.height );
144154 }
0 commit comments