I am trying to use the scrollViewWillEndDragging: method to to roll my own paged UICollectionView with previews on either side similar to the App Store app. However using the code below the scrollview will only scroll to the desired rect if i stop dragging with no inertia (ie. just lift up my finger). If I flick with any amount of inertia the method still gets called but the scrollview does not scroll the desired rect, it just keeps scrolling?
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
int itemWidth = 280;
MyCollectionView *collectionView = (MyIndexedCollectionView *) scrollView;
int totalPages = [self.colorArray[collectionView.index] count];
int currentPage;
int nextPage;
if (lastContentOffset < (int)scrollView.contentOffset.x)
{
// moved right
NSLog(@"scrolling right");
double currentPageOffset = ceil((scrollView.contentSize.width -
scrollView.contentOffset.x) / itemWidth);
currentPage = totalPages - currentPageOffset;
nextPage = currentPage >= totalPages ? totalPages : currentPage + 1;
}
else if (lastContentOffset > (int)scrollView.contentOffset.x)
{
// moved left
NSLog(@"scrolling left");
double currentPageOffset = floor((scrollView.contentSize.width -
scrollView.contentOffset.x) / itemWidth);
currentPage = totalPages - currentPageOffset;
nextPage = currentPage <= 0 ? 0 : currentPage - 1;
}
int xOffset = (nextPage * itemWidth);
int nextOffsetPage = (totalPages - ((scrollView.contentSize.width - xOffset) /
itemWidth)) + 1;
[scrollView scrollRectToVisible:CGRectMake(xOffset,
0,
collectionView.bounds.size.width,
collectionView.bounds.size.height)
animated:YES];
}
After giving up on this method I tried using the scrollViewWillBeginDecelerating: method instead and the exact same code works perfectly?? The reason why i am looking to use the scrollViewWillEndDragging: method instead is for two reasons:
scrollViewWillBeginDecelerating: method does not get called if you stop dragging by lifting your finger without any inertia.Any ideas, am I misunderstanding what this callback is doing?
DOH!! Turns out i should have been using the targetContentOffset to set the offset i wanted to scroll to instead of scrollToRectToVisible: ala:
*targetContentOffset = CGPointMake(myTargetOffset, targetContentOffset->y);
RTFM :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With