Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1365 from facebook/ASCellNodeVisibilitySync
Browse files Browse the repository at this point in the history
[ASCellNode] Upgrades to ASCellNodeVisibilityEvent to ensure it is always synchronized with visibilityDidChange:
  • Loading branch information
appleguy committed Mar 11, 2016
2 parents c2b0670 + ff8ffff commit 6bd4a75
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 31 deletions.
5 changes: 5 additions & 0 deletions AsyncDisplayKit/ASCellNode+Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
*/
@property (nonatomic, weak) id<ASCellNodeLayoutDelegate> layoutDelegate;

/*
* Back-pointer to the containing scrollView instance, set only for visible cells. Used for Cell Visibility Event callbacks.
*/
@property (nonatomic, weak) UIScrollView *scrollView;

@end
14 changes: 14 additions & 0 deletions AsyncDisplayKit/ASCellNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ - (void)cellNodeVisibilityEvent:(ASCellNodeVisibilityEvent)event
// To be overriden by subclasses
}

- (void)visibilityDidChange:(BOOL)isVisible
{
[super visibilityDidChange:isVisible];

CGRect cellFrame = CGRectZero;
if (_scrollView) {
// It is not safe to message nil with a structure return value, so ensure our _scrollView has not died.
cellFrame = [self.view convertRect:self.bounds toView:_scrollView];
}
[self cellNodeVisibilityEvent:isVisible ? ASCellNodeVisibilityEventVisible : ASCellNodeVisibilityEventInvisible
inScrollView:_scrollView
withCellFrame:cellFrame];
}

@end


Expand Down
24 changes: 11 additions & 13 deletions AsyncDisplayKit/ASCollectionView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -532,39 +532,35 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(_ASCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];
ASCellNode *cellNode = [cell node];
cellNode.scrollView = collectionView;

if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) {
[_asyncDelegate collectionView:self willDisplayNodeForItemAtIndexPath:indexPath];
}

ASCellNode *cellNode = [cell node];
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];

if (cellNode.neverShowPlaceholders) {
[cellNode recursivelyEnsureDisplaySynchronously:YES];
}
if (ASSubclassOverridesSelector([ASCellNode class], [cellNode class], @selector(cellNodeVisibilityEvent:inScrollView:withCellFrame:))) {
[_cellsForVisibilityUpdates addObject:cell];
[cellNode cellNodeVisibilityEvent:ASCellNodeVisibilityEventVisible
inScrollView:collectionView
withCellFrame:cell.frame];
}
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(_ASCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];

ASCellNode *cellNode = [cell node];

if ([_asyncDelegate respondsToSelector:@selector(collectionView:didEndDisplayingNode:forItemAtIndexPath:)]) {
ASCellNode *node = ((_ASCollectionViewCell *)cell).node;
ASDisplayNodeAssertNotNil(node, @"Expected node associated with removed cell not to be nil.");
[_asyncDelegate collectionView:self didEndDisplayingNode:node forItemAtIndexPath:indexPath];
ASDisplayNodeAssertNotNil(cellNode, @"Expected node associated with removed cell not to be nil.");
[_asyncDelegate collectionView:self didEndDisplayingNode:cellNode forItemAtIndexPath:indexPath];
}

if ([_cellsForVisibilityUpdates containsObject:cell]) {
ASCellNode *node = ((_ASCollectionViewCell *)cell).node;
[node cellNodeVisibilityEvent:ASCellNodeVisibilityEventInvisible
inScrollView:collectionView
withCellFrame:cell.frame];
[_cellsForVisibilityUpdates removeObject:cell];
}

Expand All @@ -574,6 +570,8 @@ - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(
[_asyncDelegate collectionView:self didEndDisplayingNodeForItemAtIndexPath:indexPath];
}
#pragma clang diagnostic pop

cellNode.scrollView = nil;
}

#pragma mark -
Expand Down
32 changes: 15 additions & 17 deletions AsyncDisplayKit/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -615,46 +615,42 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView
- (void)tableView:(UITableView *)tableView willDisplayCell:(_ASTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
_pendingVisibleIndexPath = indexPath;

[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];

ASCellNode *cellNode = [cell node];
cellNode.scrollView = tableView;

if ([_asyncDelegate respondsToSelector:@selector(tableView:willDisplayNodeForRowAtIndexPath:)]) {
[_asyncDelegate tableView:self willDisplayNodeForRowAtIndexPath:indexPath];
}

[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];

ASCellNode *cellNode = [cell node];

if (ASSubclassOverridesSelector([ASCellNode class], [cellNode class], @selector(cellNodeVisibilityEvent:inScrollView:withCellFrame:))) {
[_cellsForVisibilityUpdates addObject:cell];
[cellNode cellNodeVisibilityEvent:ASCellNodeVisibilityEventVisible
inScrollView:tableView
withCellFrame:cell.frame];
}
if (cellNode.neverShowPlaceholders) {
[cellNode recursivelyEnsureDisplaySynchronously:YES];
}

if (ASSubclassOverridesSelector([ASCellNode class], [cellNode class], @selector(cellNodeVisibilityEvent:inScrollView:withCellFrame:))) {
[_cellsForVisibilityUpdates addObject:cell];
}
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(_ASTableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
if ([_pendingVisibleIndexPath isEqual:indexPath]) {
_pendingVisibleIndexPath = nil;
}

ASCellNode *cellNode = [cell node];

[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];

if ([_asyncDelegate respondsToSelector:@selector(tableView:didEndDisplayingNode:forRowAtIndexPath:)]) {
ASCellNode *node = ((_ASTableViewCell *)cell).node;
ASDisplayNodeAssertNotNil(node, @"Expected node associated with removed cell not to be nil.");
[_asyncDelegate tableView:self didEndDisplayingNode:node forRowAtIndexPath:indexPath];
ASDisplayNodeAssertNotNil(cellNode, @"Expected node associated with removed cell not to be nil.");
[_asyncDelegate tableView:self didEndDisplayingNode:cellNode forRowAtIndexPath:indexPath];
}

if ([_cellsForVisibilityUpdates containsObject:cell]) {
[_cellsForVisibilityUpdates removeObject:cell];
ASCellNode *node = ((_ASTableViewCell *)cell).node;
[node cellNodeVisibilityEvent:ASCellNodeVisibilityEventInvisible
inScrollView:tableView
withCellFrame:cell.frame];
}

#pragma clang diagnostic push
Expand All @@ -663,6 +659,8 @@ - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(_ASTableViewCel
[_asyncDelegate tableView:self didEndDisplayingNodeForRowAtIndexPath:indexPath];
}
#pragma clang diagnostic pop

cellNode.scrollView = nil;
}


Expand Down
3 changes: 2 additions & 1 deletion AsyncDisplayKit/Details/ASRangeController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ - (void)visibleNodeIndexPathsDidChangeWithScrollDirection:(ASScrollDirection)scr
{
_scrollDirection = scrollDirection;

[self scheduleRangeUpdate];
// Perform update immediately, so that cells receive a visibilityDidChange: call before their first pixel is visible.
[self performRangeUpdate];
}

- (void)updateCurrentRangeWithMode:(ASLayoutRangeMode)rangeMode
Expand Down

0 comments on commit 6bd4a75

Please sign in to comment.