I'm having trouble getting targetContentOffsetForProposedContentOffset:withScrollingVelocity:
to work on a custom UICollectionViewFlowLayout.
I want a very basic modification where the collection view stops the 'page scrolling' every 936 pixels (instead of 1024), this is for an ipad on landscape mode.
For this i setup my collection view like so...
-(void)setup
{
TvGuideFlowLayout *flowLayout = [[TvGuideFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[flowLayout setMinimumInteritemSpacing:0.0f];
[flowLayout setMinimumLineSpacing:0.0f];
[flowLayout setItemSize:CGSizeMake(234, 768)];
[self.collectionView registerClass:[TvGuideCollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.collectionView setPagingEnabled:YES];
[self.collectionView setCollectionViewLayout:flowLayout];
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.collectionView reloadData];
}
The code bellow gets called correctly but the result is 'ignored' or something because the page scroll still occurs every 1024px.
TvGuideFlowLayout.m
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
float offsetX = proposedContentOffset.x / 1024;
offsetX *= 936;
CGPoint result = CGPointMake( offsetX, 0);
return result;
}
I tried using a negative value and still it did the 1024 thing.
I tried giving it a very short offset (every 468px) and the collection view doesn't scroll at all, it just snapped back to the start position.
Why doesn't this just work? it seems that the result of this call is only used a suggestion or only if some other condition is met.
You've set pagingEnabled
on the collection view, which is probably overriding anything you are doing with the layout - this property makes the collection view stop scrolling at the multiples of the content size.
You've got two ways around this:
pagingEnabled
and use the scroll view delegate method - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
. The drawback of this is that the snapping behaviour and feel from paging is very finely tuned and you may struggle to replicate it exactly. pagingEnabled
. The drawback here is that you have to make the collection view the size of the "page" of content, which may not be what you want (presumably you want to see part of the other "pages" to the left and right?). You can work around this by preventing clipping the subviews of the collection view but this can interfere with cell reuse and you can get some strange visual artefacts, depending how many cells are on a page.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