ASPagerFlowLayout.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // ASPagerFlowLayout.m
  3. // AsyncDisplayKit
  4. //
  5. // Created by Levi McCallum on 2/12/16.
  6. //
  7. // Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  8. // This source code is licensed under the BSD-style license found in the
  9. // LICENSE file in the root directory of this source tree. An additional grant
  10. // of patent rights can be found in the PATENTS file in the same directory.
  11. //
  12. #import <AsyncDisplayKit/ASPagerFlowLayout.h>
  13. #import <AsyncDisplayKit/ASCellNode.h>
  14. #import <AsyncDisplayKit/ASCollectionView.h>
  15. @interface ASPagerFlowLayout () {
  16. __weak ASCellNode *_currentCellNode;
  17. }
  18. @end
  19. @implementation ASPagerFlowLayout
  20. - (ASCollectionView *)asCollectionView
  21. {
  22. // Dynamic cast is too slow and not worth it.
  23. return (ASCollectionView *)self.collectionView;
  24. }
  25. - (void)prepareLayout
  26. {
  27. [super prepareLayout];
  28. if (_currentCellNode == nil) {
  29. [self _updateCurrentNode];
  30. }
  31. }
  32. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset
  33. {
  34. // Don't mess around if the user is interacting with the page node. Although if just a rotation happened we should
  35. // try to use the current index path to not end up setting the target content offset to something in between pages
  36. if (!self.collectionView.decelerating && !self.collectionView.tracking) {
  37. NSIndexPath *indexPath = [self.asCollectionView indexPathForNode:_currentCellNode];
  38. if (indexPath) {
  39. return [self _targetContentOffsetForItemAtIndexPath:indexPath proposedContentOffset:proposedContentOffset];
  40. }
  41. }
  42. return [super targetContentOffsetForProposedContentOffset:proposedContentOffset];
  43. }
  44. - (CGPoint)_targetContentOffsetForItemAtIndexPath:(NSIndexPath *)indexPath proposedContentOffset:(CGPoint)proposedContentOffset
  45. {
  46. if ([self _dataSourceIsEmpty]) {
  47. return proposedContentOffset;
  48. }
  49. UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:indexPath];
  50. if (attributes == nil) {
  51. return proposedContentOffset;
  52. }
  53. CGFloat xOffset = (CGRectGetWidth(self.collectionView.bounds) - CGRectGetWidth(attributes.frame)) / 2.0;
  54. return CGPointMake(attributes.frame.origin.x - xOffset, proposedContentOffset.y);
  55. }
  56. - (BOOL)_dataSourceIsEmpty
  57. {
  58. return ([self.collectionView numberOfSections] == 0 ||
  59. [self.collectionView numberOfItemsInSection:0] == 0);
  60. }
  61. - (void)_updateCurrentNode
  62. {
  63. // Never change node during an animated bounds change (rotation)
  64. // NOTE! Listening for -prepareForAnimatedBoundsChange and -finalizeAnimatedBoundsChange
  65. // isn't sufficient here! It's broken!
  66. NSArray *animKeys = self.collectionView.layer.animationKeys;
  67. for (NSString *key in animKeys) {
  68. if ([key hasPrefix:@"bounds"]) {
  69. return;
  70. }
  71. }
  72. CGRect bounds = self.collectionView.bounds;
  73. CGRect rect = CGRectMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds), 1, 1);
  74. NSIndexPath *indexPath = [self layoutAttributesForElementsInRect:rect].firstObject.indexPath;
  75. if (indexPath) {
  76. ASCellNode *node = [self.asCollectionView nodeForItemAtIndexPath:indexPath];
  77. if (node) {
  78. _currentCellNode = node;
  79. }
  80. }
  81. }
  82. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
  83. {
  84. [self _updateCurrentNode];
  85. return [super shouldInvalidateLayoutForBoundsChange:newBounds];
  86. }
  87. @end