From 7249661990085606a11ccdfb30b1b1abdfe2a38e Mon Sep 17 00:00:00 2001 From: Huy Nguyen Date: Sun, 9 Aug 2015 23:18:25 +0300 Subject: [PATCH] Improve relayout when constrained size of all nodes is changed: - In -layoutSubviews of table and collection views, detect changes that cause a different constrained size for nodes, and trigger relayout immediately. - Orientation change can be handled by this solution. So, no need to observe to its events. - Update Kittens example to support iPad (easier to catch bugs on these devices) and add a title to navigation bar (looks a bit nicer). --- AsyncDisplayKit/ASCollectionView.mm | 38 ++++--------------- AsyncDisplayKit/ASTableView.mm | 33 +++------------- AsyncDisplayKit/Details/ASDataController.h | 4 +- AsyncDisplayKit/Details/ASDataController.mm | 17 ++++----- AsyncDisplayKit/Private/ASInternalHelpers.h | 4 -- AsyncDisplayKit/Private/ASInternalHelpers.mm | 15 -------- .../Kittens/Sample.xcodeproj/project.pbxproj | 2 + examples/Kittens/Sample/ViewController.m | 1 + 8 files changed, 27 insertions(+), 87 deletions(-) diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index d6fa28ebc2..8223d5208e 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -125,7 +125,7 @@ static BOOL _isInterceptedSelector(SEL sel) ASBatchContext *_batchContext; - BOOL _pendingRelayoutForAllRows; + CGSize _maxSizeForNodesConstrainedSize; } @property (atomic, assign) BOOL asyncDataSourceLocked; @@ -170,16 +170,13 @@ static BOOL _isInterceptedSelector(SEL sel) _performingBatchUpdates = NO; _batchUpdateBlocks = [NSMutableArray array]; + + _collectionViewLayoutImplementsInsetSection = [layout respondsToSelector:@selector(sectionInset)]; + + _maxSizeForNodesConstrainedSize = self.bounds.size; [self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"_ASCollectionViewCell"]; - if (ASSystemVersionLessThan8()) { - [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; - } - - _collectionViewLayoutImplementsInsetSection = [layout respondsToSelector:@selector(sectionInset)]; - return self; } @@ -189,11 +186,6 @@ static BOOL _isInterceptedSelector(SEL sel) // This bug might be iOS 7-specific. super.delegate = nil; super.dataSource = nil; - - if (ASSystemVersionLessThan8()) { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; - } } #pragma mark - @@ -480,26 +472,12 @@ static BOOL _isInterceptedSelector(SEL sel) } } - -#pragma mark - -#pragma mark Orientation Change Handling - -- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection -{ - _pendingRelayoutForAllRows = YES; -} - -- (void)deviceOrientationDidChange -{ - _pendingRelayoutForAllRows = YES; -} - - (void)layoutSubviews { [super layoutSubviews]; - if (_pendingRelayoutForAllRows) { - _pendingRelayoutForAllRows = NO; + if (! CGSizeEqualToSize(_maxSizeForNodesConstrainedSize, self.bounds.size)) { + _maxSizeForNodesConstrainedSize = self.bounds.size; [self performBatchAnimated:NO updates:^{ [_dataController relayoutAllRows]; } completion:nil]; @@ -562,7 +540,7 @@ static BOOL _isInterceptedSelector(SEL sel) if (_asyncDataSourceImplementsConstrainedSizeForNode) { constrainedSize = [_asyncDataSource collectionView:self constrainedSizeForNodeAtIndexPath:indexPath]; } else { - CGSize maxSize = self.bounds.size; + CGSize maxSize = _maxSizeForNodesConstrainedSize; if (ASScrollDirectionContainsHorizontalDirection([self scrollableDirections])) { maxSize.width = FLT_MAX; } else { diff --git a/AsyncDisplayKit/ASTableView.mm b/AsyncDisplayKit/ASTableView.mm index cf45cc5320..6a9a3bd339 100644 --- a/AsyncDisplayKit/ASTableView.mm +++ b/AsyncDisplayKit/ASTableView.mm @@ -146,9 +146,9 @@ static BOOL _isInterceptedSelector(SEL sel) NSIndexPath *_contentOffsetAdjustmentTopVisibleRow; CGFloat _contentOffsetAdjustment; - BOOL _pendingRelayoutForAllRows; - BOOL _asyncDataSourceImplementsConstrainedSizeForNode; + + CGFloat _maxWidthForNodesConstrainedSize; } @property (atomic, assign) BOOL asyncDataSourceLocked; @@ -202,10 +202,7 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) { _automaticallyAdjustsContentOffset = NO; - if (ASSystemVersionLessThan8()) { - [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; - } + _maxWidthForNodesConstrainedSize = self.bounds.size.width; } - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style @@ -243,11 +240,6 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) { // This bug might be iOS 7-specific. super.delegate = nil; super.dataSource = nil; - - if (ASSystemVersionLessThan8()) { - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; - } } #pragma mark - @@ -373,25 +365,12 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) { [_dataController endUpdatesAnimated:animated completion:completion]; } -#pragma mark - -#pragma mark Orientation Change Handling - -- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection -{ - _pendingRelayoutForAllRows = YES; -} - -- (void)deviceOrientationDidChange -{ - _pendingRelayoutForAllRows = YES; -} - - (void)layoutSubviews { [super layoutSubviews]; - if (_pendingRelayoutForAllRows) { - _pendingRelayoutForAllRows = NO; + if (_maxWidthForNodesConstrainedSize != self.bounds.size.width) { + _maxWidthForNodesConstrainedSize = self.bounds.size.width; [self beginUpdates]; [_dataController relayoutAllRows]; [self endUpdates]; @@ -812,7 +791,7 @@ void ASPerformBlockWithoutAnimation(BOOL withoutAnimation, void (^block)()) { } // Default size range - return ASSizeRangeMake(CGSizeZero, CGSizeMake(self.bounds.size.width, FLT_MAX)); + return ASSizeRangeMake(CGSizeZero, CGSizeMake(_maxWidthForNodesConstrainedSize, FLT_MAX)); } - (void)dataControllerLockDataSource diff --git a/AsyncDisplayKit/Details/ASDataController.h b/AsyncDisplayKit/Details/ASDataController.h index 7221d688cf..68e586daa8 100644 --- a/AsyncDisplayKit/Details/ASDataController.h +++ b/AsyncDisplayKit/Details/ASDataController.h @@ -156,8 +156,8 @@ typedef NSUInteger ASDataControllerAnimationOptions; - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withAnimationOptions:(ASDataControllerAnimationOptions)animationOptions; /** - * Re-measures all loaded nodes. Used for external relayout (relayout that is caused by a change in constrained size of each and every cell node, - * for example, after an orientation change). + * Re-measures all loaded nodes. Used to respond to a change in size of the containing view + * (e.g. ASTableView or ASCollectionView after an orientation change). */ - (void)relayoutAllRows; diff --git a/AsyncDisplayKit/Details/ASDataController.mm b/AsyncDisplayKit/Details/ASDataController.mm index 4d3d42fcc7..4cfc03270d 100644 --- a/AsyncDisplayKit/Details/ASDataController.mm +++ b/AsyncDisplayKit/Details/ASDataController.mm @@ -564,15 +564,14 @@ static void *kASSizingQueueContext = &kASSizingQueueContext; LOG(@"Edit Command - relayoutRows"); [_editingTransactionQueue waitUntilAllOperationsAreFinished]; - NSArray *indexPaths = ASIndexPathsForMultidimensionalArray(_completedNodes); - NSArray *loadedNodes = ASFindElementsInMultidimensionalArrayAtIndexPaths(_completedNodes, indexPaths); - - for (NSUInteger i = 0; i < loadedNodes.count && i < indexPaths.count; i++) { - ASSizeRange constrainedSize = [_dataSource dataController:self constrainedSizeForNodeAtIndexPath:indexPaths[i]]; - ASCellNode *node = loadedNodes[i]; - [node measureWithSizeRange:constrainedSize]; - node.frame = CGRectMake(0.0f, 0.0f, node.calculatedSize.width, node.calculatedSize.height); - } + [_completedNodes enumerateObjectsUsingBlock:^(NSMutableArray *section, NSUInteger sectionIndex, BOOL *stop) { + [section enumerateObjectsUsingBlock:^(ASCellNode *node, NSUInteger rowIndex, BOOL *stop) { + ASSizeRange constrainedSize = [_dataSource dataController:self + constrainedSizeForNodeAtIndexPath:[NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex]]; + [node measureWithSizeRange:constrainedSize]; + node.frame = CGRectMake(0.0f, 0.0f, node.calculatedSize.width, node.calculatedSize.height); + }]; + }]; }]; } diff --git a/AsyncDisplayKit/Private/ASInternalHelpers.h b/AsyncDisplayKit/Private/ASInternalHelpers.h index 8693440eff..e20c8c999a 100644 --- a/AsyncDisplayKit/Private/ASInternalHelpers.h +++ b/AsyncDisplayKit/Private/ASInternalHelpers.h @@ -24,8 +24,4 @@ CGFloat ASCeilPixelValue(CGFloat f); CGFloat ASRoundPixelValue(CGFloat f); -BOOL ASSystemVersionLessThan8(); - -BOOL ASSystemVersionLessThanVersion(NSString *version); - ASDISPLAYNODE_EXTERN_C_END diff --git a/AsyncDisplayKit/Private/ASInternalHelpers.mm b/AsyncDisplayKit/Private/ASInternalHelpers.mm index f230e1e49c..d161523f2b 100644 --- a/AsyncDisplayKit/Private/ASInternalHelpers.mm +++ b/AsyncDisplayKit/Private/ASInternalHelpers.mm @@ -61,18 +61,3 @@ CGFloat ASRoundPixelValue(CGFloat f) { return roundf(f * ASScreenScale()) / ASScreenScale(); } - -BOOL ASSystemVersionLessThan8() -{ - static BOOL lessThan8; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - lessThan8 = ASSystemVersionLessThanVersion(@"8"); - }); - return lessThan8; -} - -BOOL ASSystemVersionLessThanVersion(NSString *version) -{ - return [[[UIDevice currentDevice] systemVersion] compare:version options:NSNumericSearch] == NSOrderedAscending; -} diff --git a/examples/Kittens/Sample.xcodeproj/project.pbxproj b/examples/Kittens/Sample.xcodeproj/project.pbxproj index fde58763d9..293b513d1e 100644 --- a/examples/Kittens/Sample.xcodeproj/project.pbxproj +++ b/examples/Kittens/Sample.xcodeproj/project.pbxproj @@ -314,6 +314,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 7.1; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; @@ -326,6 +327,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 7.1; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; }; diff --git a/examples/Kittens/Sample/ViewController.m b/examples/Kittens/Sample/ViewController.m index 02760a6355..7989fe15ce 100644 --- a/examples/Kittens/Sample/ViewController.m +++ b/examples/Kittens/Sample/ViewController.m @@ -59,6 +59,7 @@ static const NSInteger kMaxLitterSize = 100; // max number of kitten cell _blurbNodeIndexPath = [NSIndexPath indexPathForItem:0 inSection:0]; + self.title = @"Kittens"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(toggleEditingMode)];