UICollectionView does not always revive deceleration when overriding scrollViewWillEndDragging

I am creating custom paging for my UICollectionView. I want some of the cells at the bottom to hang from the edge of the screen, however, with regular swapping, scrolling to the next page means that if half of the cell at the bottom of the page is displayed, it will only show the other half - on the next page. I want the cells to hang on the end, but stop paging so that the cells hanging from the screen are clear.

So, to do this, I tried the function - (void) scrollViewWillEndDragging: (UIScrollView *) scrollView withVelocity: (CGPoint) velocity targetContentOffset: (inout CGPoint *) targetContentOffset

If I drag a second or two, it sees the work as expected, however I am trying to emulate a โ€œclickโ€ that works so well when paging is turned on. When I output my UICollectionView, it goes to targetContentOffset, not the animation.

How to prevent this?

Here is my code:

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { if(targetContentOffset->y < 400) { targetContentOffset->y = 0; return; } int baseCheck = 400; while(baseCheck <= 10000) { if(targetContentOffset->y > baseCheck && targetContentOffset->y < baseCheck + 800) { targetContentOffset->y = (baseCheck + 340); return; } baseCheck += 800; } targetContentOffset->y = 0; } 
+4
source share
2 answers

I ran into the same problem and managed to handle it. In my case, I mimic a broken scrollView (which is actually a UICollectionView), where the page size is less than the size of the collection itself. I noticed that the โ€œjumpingโ€ scroll occurs under certain conditions:

 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { CGFloat pageWidth; // defined somewhere else NSInteger numberOfPages; // depends on your dataSource CGFloat proposedOffset = targetContentOffset->x; NSInteger currentPage = roundf(self.collectionView.contentOffset.x / pageWidth); NSInteger proposedPage = roundf(proposedOffset / pageWidth); // what follows is a fix for a weird case where the scroll 'jumps' into place with no animation if(currentPage == proposedPage) { if((currentPage == 0 && velocity.x > 0) || (currentPage == (numberOfPages - 1) && velocity.x < 0) || (currentPage > 0 && currentPage < (numberOfPages - 1) && fabs(velocity.x) > 0) ) { // this forces the scrolling animation to stop in its current place [self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO]; [UIView animateWithDuration:.3 delay:0. options:UIViewAnimationOptionCurveEaseOut animations:^{ [self.collectionView setContentOffset:CGPointMake(currentPage * pageWidth, 0)]; } completion:NULL]; } } targetContentOffset->x = (pageWidth * proposedPage); } 
+5
source

It โ€œjumpsโ€ directly to targetContentOffset because it uses the given speed. And the film has a very high speed. You should check the speed value and, if above a certain speed (about 2, I think, but you can configure it), you do the animation yourself (with a simple [scrollView setContentOffset:contentOffset animated:YES];

+2
source

All Articles