I'm trying to use the new scrollViewWillEndDragging:withVelocity:targetContentOffset: UIScrollView delegate call in iOS 5 but i can't seem to get it to actually respond to me correctly. I'm changing the targetContentOffset->x value but it never ends up being used. I know the code is being ran because it'll hit breakpoints in that function. I've even tried setting the offset value to a hard coded number so i'd know where it would end up but it never works.
Has anyone been able to use this correctly and make it work? Is there any other delegate call that must be implemented in order for this to work?
Here's my code in case someone sees something wrong with it:
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
// goodOffsetX returns the contentOffset i want the scrollView to stop at
CGFloat goodOffsetX = [self _horizontalContentOffsetForTargetHorizontalContentOffset:(*targetContentOffset).x velocity:velocity.x];
NSLog( @" " );
NSLog( @"scrollViewWillEndDragging" );
NSLog( @" velocity: %f", velocity.x );
NSLog( @" currentX: %f", scrollView.contentOffset.x );
NSLog( @" uikit targetX: %f", (*targetContentOffset).x );
NSLog( @" pagedX: %f", goodOffsetX );
targetContentOffset->x = goodOffsetX;
}
Swift 2.2:
I also used the
collectionView?.decelerationRate = UIScrollViewDecelerationRateFast
as suggested by @skagedal to improve page speed.I was able to run a quick test and got this to correctly fire and make my object stop as desired. I did this using the following simple test:
It seems that this method is likely not the issue in your code, but it is more likely that your 'goodOffsetX' is not correctly calculating a valid value to stop at.
SWIFT 3
With a demo here https://github.com/damienromito/CollectionViewCustom
You can implement custom paging with this code:
Of course you must set pagingEnabled = NO. _currentPage is a class iVar. Thanks to http://www.mysamplecode.com/2012/12/ios-scrollview-example-with-paging.html for pointing the right way.