From 389fd4b15db66ca07f59f64514491bea414099f3 Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Wed, 11 May 2016 02:34:26 +0200 Subject: [PATCH] Further improvements for ASPagerNode rotation --- AsyncDisplayKit/ASPagerFlowLayout.m | 39 ++++++++++++++++------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/AsyncDisplayKit/ASPagerFlowLayout.m b/AsyncDisplayKit/ASPagerFlowLayout.m index 40a7970e50..47e7f1e09e 100644 --- a/AsyncDisplayKit/ASPagerFlowLayout.m +++ b/AsyncDisplayKit/ASPagerFlowLayout.m @@ -8,23 +8,22 @@ #import "ASPagerFlowLayout.h" -@interface ASPagerFlowLayout () - -@property (strong, nonatomic) NSIndexPath *currentIndexPath; +@interface ASPagerFlowLayout () { + BOOL _didRotate; + CGRect _cachedCollectionViewBounds; + NSIndexPath *_currentIndexPath; +} @end -@implementation ASPagerFlowLayout { - BOOL _didRotate; - CGRect _cachedCollectionViewBounds; -} +@implementation ASPagerFlowLayout - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity { - NSInteger currentPage = ceil(proposedContentOffset.x / self.collectionView.bounds.size.width); - self.currentIndexPath = [NSIndexPath indexPathForItem:currentPage inSection:0]; - - return [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity]; + NSInteger currentPage = ceil(proposedContentOffset.x / self.collectionView.bounds.size.width); + _currentIndexPath = [NSIndexPath indexPathForItem:currentPage inSection:0]; + + return [super targetContentOffsetForProposedContentOffset:proposedContentOffset withScrollingVelocity:velocity]; } @@ -42,7 +41,7 @@ NSArray *layoutAttributes = [self layoutAttributesForElementsInRect:visibleRect]; for (UICollectionViewLayoutAttributes *attributes in layoutAttributes) { if ([attributes representedElementCategory] == UICollectionElementCategoryCell && attributes.center.x == visibleXCenter) { - self.currentIndexPath = attributes.indexPath; + _currentIndexPath = attributes.indexPath; break; } } @@ -58,8 +57,8 @@ // try to use the current index path to not end up setting the target content offset to something in between pages if (_didRotate || (!self.collectionView.isDecelerating && !self.collectionView.isTracking)) { _didRotate = NO; - if (self.currentIndexPath) { - return [self _targetContentOffsetForItemAtIndexPath:self.currentIndexPath proposedContentOffset:proposedContentOffset]; + if (_currentIndexPath) { + return [self _targetContentOffsetForItemAtIndexPath:_currentIndexPath proposedContentOffset:proposedContentOffset]; } } @@ -72,13 +71,19 @@ return proposedContentOffset; } - CGFloat currentPageXOffset = self.currentIndexPath.item * CGRectGetWidth(self.collectionView.bounds); - return CGPointMake(currentPageXOffset, proposedContentOffset.y); + UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:_currentIndexPath]; + if (attributes == nil) { + return proposedContentOffset; + } + + CGFloat xOffset = (CGRectGetWidth(self.collectionView.bounds) - CGRectGetWidth(attributes.frame)) / 2.0; + return CGPointMake(attributes.frame.origin.x - xOffset, proposedContentOffset.y); } - (BOOL)_dataSourceIsEmpty { - return ([self.collectionView numberOfSections] == 0 || [self.collectionView numberOfItemsInSection:0] == 0); + return ([self.collectionView numberOfSections] == 0 || + [self.collectionView numberOfItemsInSection:0] == 0); } @end