Observe decelerating scroll velocity for accurate direction reporting

This commit is contained in:
Levi McCallum
2016-01-27 23:19:23 -08:00
parent e22c50101a
commit 6eb0521fa3
2 changed files with 35 additions and 7 deletions

View File

@@ -88,6 +88,8 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
NSMutableSet *_registeredSupplementaryKinds;
CGPoint _deceleratingVelocity;
/**
* If YES, the `UICollectionView` will reload its data on next layout pass so we should not forward any updates to it.
@@ -516,11 +518,16 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (ASScrollDirection)scrollDirection
{
CGPoint scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview];
return [self scrollDirectionForVelocity:scrollVelocity];
CGPoint scrollVelocity;
if (self.isTracking) {
scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview];
} else {
scrollVelocity = _deceleratingVelocity;
}
return [self _scrollDirectionForVelocity:scrollVelocity];
}
- (ASScrollDirection)scrollDirectionForVelocity:(CGPoint)scrollVelocity
- (ASScrollDirection)_scrollDirectionForVelocity:(CGPoint)scrollVelocity
{
ASScrollDirection direction = ASScrollDirectionNone;
ASScrollDirection scrollableDirections = [self scrollableDirections];
@@ -570,7 +577,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection];
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:[self scrollDirection]];
if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) {
[_asyncDelegate collectionView:self willDisplayNodeForItemAtIndexPath:indexPath];
@@ -629,6 +636,11 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
_deceleratingVelocity = CGPointMake(
scrollView.contentOffset.x - targetContentOffset->x,
scrollView.contentOffset.y - targetContentOffset->y
);
[self handleBatchFetchScrollingToOffset:*targetContentOffset];
if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {

View File

@@ -106,6 +106,8 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
NSIndexPath *_contentOffsetAdjustmentTopVisibleRow;
CGFloat _contentOffsetAdjustment;
CGPoint _deceleratingVelocity;
CGFloat _nodesConstrainedWidth;
BOOL _ignoreNodesConstrainedWidthChange;
BOOL _queuedNodeHeightUpdate;
@@ -563,14 +565,23 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
- (ASScrollDirection)scrollDirection
{
CGPoint scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview];
CGPoint scrollVelocity;
if (self.isTracking) {
scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview];
} else {
scrollVelocity = _deceleratingVelocity;
}
return [self _scrollDirectionForVelocity:scrollVelocity];
}
- (ASScrollDirection)_scrollDirectionForVelocity:(CGPoint)velocity
{
ASScrollDirection direction = ASScrollDirectionNone;
if (scrollVelocity.y > 0) {
if (velocity.y > 0) {
direction = ASScrollDirectionDown;
} else {
direction = ASScrollDirectionUp;
}
return direction;
}
@@ -618,6 +629,11 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
_deceleratingVelocity = CGPointMake(
scrollView.contentOffset.x - targetContentOffset->x,
scrollView.contentOffset.y - targetContentOffset->y
);
[self handleBatchFetchScrollingToOffset:*targetContentOffset];
if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {