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; 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. * 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 - (ASScrollDirection)scrollDirection
{ {
CGPoint scrollVelocity = [self.panGestureRecognizer velocityInView:self.superview]; CGPoint scrollVelocity;
return [self scrollDirectionForVelocity: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 direction = ASScrollDirectionNone;
ASScrollDirection scrollableDirections = [self scrollableDirections]; ASScrollDirection scrollableDirections = [self scrollableDirections];
@@ -570,7 +577,7 @@ static NSString * const kCellReuseIdentifier = @"_ASCollectionViewCell";
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{ {
[_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:self.scrollDirection]; [_rangeController visibleNodeIndexPathsDidChangeWithScrollDirection:[self scrollDirection]];
if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) { if ([_asyncDelegate respondsToSelector:@selector(collectionView:willDisplayNodeForItemAtIndexPath:)]) {
[_asyncDelegate collectionView:self willDisplayNodeForItemAtIndexPath:indexPath]; [_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 - (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]; [self handleBatchFetchScrollingToOffset:*targetContentOffset];
if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) { if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {

View File

@@ -105,6 +105,8 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
NSIndexPath *_contentOffsetAdjustmentTopVisibleRow; NSIndexPath *_contentOffsetAdjustmentTopVisibleRow;
CGFloat _contentOffsetAdjustment; CGFloat _contentOffsetAdjustment;
CGPoint _deceleratingVelocity;
CGFloat _nodesConstrainedWidth; CGFloat _nodesConstrainedWidth;
BOOL _ignoreNodesConstrainedWidthChange; BOOL _ignoreNodesConstrainedWidthChange;
@@ -563,14 +565,23 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
- (ASScrollDirection)scrollDirection - (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; ASScrollDirection direction = ASScrollDirectionNone;
if (scrollVelocity.y > 0) { if (velocity.y > 0) {
direction = ASScrollDirectionDown; direction = ASScrollDirectionDown;
} else { } else {
direction = ASScrollDirectionUp; direction = ASScrollDirectionUp;
} }
return direction; return direction;
} }
@@ -618,6 +629,11 @@ static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset - (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]; [self handleBatchFetchScrollingToOffset:*targetContentOffset];
if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) { if ([_asyncDelegate respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)]) {