From 382509fac84d0e39fdcd993e50c1e292788fbe01 Mon Sep 17 00:00:00 2001 From: Max Gu Date: Fri, 19 Feb 2016 21:30:36 -0800 Subject: [PATCH] Letting ASCellNode and its subclasses handle didScroll --- AsyncDisplayKit/ASCellNode+Internal.h | 15 +++++++++++++++ AsyncDisplayKit/ASCellNode.h | 10 ---------- AsyncDisplayKit/ASCellNode.m | 14 ++------------ AsyncDisplayKit/ASCollectionView.mm | 15 ++++++++++----- 4 files changed, 27 insertions(+), 27 deletions(-) create mode 100644 AsyncDisplayKit/ASCellNode+Internal.h diff --git a/AsyncDisplayKit/ASCellNode+Internal.h b/AsyncDisplayKit/ASCellNode+Internal.h new file mode 100644 index 0000000000..2152d91bc3 --- /dev/null +++ b/AsyncDisplayKit/ASCellNode+Internal.h @@ -0,0 +1,15 @@ +// +// ASCellNode+Internal.h +// Pods +// +// Created by Max Gu on 2/19/16. +// +// + +#import "ASCellNode.h" + +@interface ASCellNode (Internal) + +- (void)_visibleNodeDidScroll:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame; + +@end diff --git a/AsyncDisplayKit/ASCellNode.h b/AsyncDisplayKit/ASCellNode.h index 45da0fd1ce..830ed49e8f 100644 --- a/AsyncDisplayKit/ASCellNode.h +++ b/AsyncDisplayKit/ASCellNode.h @@ -25,15 +25,6 @@ typedef NSUInteger ASCellNodeAnimation; */ - (void)nodeDidRelayout:(ASCellNode *)node sizeChanged:(BOOL)sizeChanged; -@optional -/** - * Notifies the delegate that the specified cell node has scrolled - * - * @param node: A node informing the delegate about the scroll - * @param cellFrame: The frame of the cell that has just scrolled - */ -- (void)visibleNodeDidScroll:(ASCellNode *)node inScrollView:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame; - @end /** @@ -120,7 +111,6 @@ typedef NSUInteger ASCellNodeAnimation; - (instancetype)initWithViewControllerBlock:(ASDisplayNodeViewControllerBlock)viewControllerBlock didLoadBlock:(ASDisplayNodeDidLoadBlock)didLoadBlock; - (void)_visibleNodeDidScroll:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame; - @end diff --git a/AsyncDisplayKit/ASCellNode.m b/AsyncDisplayKit/ASCellNode.m index 7aa83fc9fe..c007110680 100644 --- a/AsyncDisplayKit/ASCellNode.m +++ b/AsyncDisplayKit/ASCellNode.m @@ -9,7 +9,6 @@ #import "ASCellNode.h" #import "ASInternalHelpers.h" -#import "ASCollectionView.h" #import #import #import @@ -24,7 +23,6 @@ UIViewController *_viewController; ASDisplayNode *_viewControllerNode; } -@property (nonatomic, assign, readwrite) BOOL shouldMonitorScrollViewDidScroll; @end @@ -38,9 +36,6 @@ // Use UITableViewCell defaults _selectionStyle = UITableViewCellSelectionStyleDefault; self.clipsToBounds = YES; - if ([self.layoutDelegate respondsToSelector:@selector(visibleNodeDidScroll:inScrollView:withCellFrame:)]) { - self.shouldMonitorScrollViewDidScroll = YES; - } return self; } @@ -60,10 +55,7 @@ [self addSubnode:_viewControllerNode]; } - - if ([self.layoutDelegate respondsToSelector:@selector(visibleNodeDidScroll:inScrollView:withCellFrame:)]) { - self.shouldMonitorScrollViewDidScroll = YES; - } + return self; } @@ -143,9 +135,7 @@ - (void)_visibleNodeDidScroll:(UIScrollView *)scrollView withCellFrame:(CGRect)cellFrame { - if (self.shouldMonitorScrollViewDidScroll) { - [self.layoutDelegate visibleNodeDidScroll:self inScrollView:scrollView withCellFrame:cellFrame]; - } + // To be overriden by subclasses } @end diff --git a/AsyncDisplayKit/ASCollectionView.mm b/AsyncDisplayKit/ASCollectionView.mm index 461fe2e702..eb456bba15 100644 --- a/AsyncDisplayKit/ASCollectionView.mm +++ b/AsyncDisplayKit/ASCollectionView.mm @@ -9,6 +9,7 @@ #import "ASAssert.h" #import "ASBatchFetching.h" #import "ASDelegateProxy.h" +#import "ASCellNode+Internal.h" #import "ASCollectionNode.h" #import "ASCollectionDataController.h" #import "ASCollectionViewLayoutController.h" @@ -541,7 +542,9 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; if (cellNode.neverShowPlaceholders) { [cellNode recursivelyEnsureDisplaySynchronously:YES]; } - [_cellsForVisibilityUpdates addObject:cell]; + if (ASSubclassOverridesSelector([ASCellNode class], [cellNode class], @selector(_visibleNodeDidScroll:withCellFrame:))) { + [_cellsForVisibilityUpdates addObject:cell]; + } } - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath @@ -553,7 +556,10 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; ASDisplayNodeAssertNotNil(node, @"Expected node associated with removed cell not to be nil."); [_asyncDelegate collectionView:self didEndDisplayingNode:node forItemAtIndexPath:indexPath]; } - [_cellsForVisibilityUpdates removeObject:cell]; + if ([_cellsForVisibilityUpdates containsObject:cell]) { + [_cellsForVisibilityUpdates removeObject:cell]; + } + #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNodeForItemAtIndexPath:)]) { @@ -674,9 +680,8 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell"; { for (_ASCollectionViewCell *collectionCell in _cellsForVisibilityUpdates) { ASCellNode *node = [collectionCell node]; - if (node.shouldMonitorScrollViewDidScroll) { - [node _visibleNodeDidScroll:scrollView withCellFrame: node.frame]; - } + // Only nodes that respond to the selector are added to _cellsForVisibilityUpdates + [node _visibleNodeDidScroll:scrollView withCellFrame:collectionCell.frame]; } if (_asyncDelegateImplementsScrollviewDidScroll) { [_asyncDelegate scrollViewDidScroll:scrollView];