Wednesday, 21 August 2013

Double custom paging with CollectionView in CollectionView

Double custom paging with CollectionView in CollectionView

I have a problem, I have a main MainCollectionView (with Horizontal
scroll) where in every cell i put another subCollectionView (with Vertical
scroll). the problem is: I have to preload the next and last cell of every
current cell for doesn't have the lag when i scroll from a cell to another
one. I want also that this scroll is a paging, then a lot of try I choose
this solution: I create every cell with 2 pixel less, with that i can load
automatically the last a first cell because i see 1px of last cell and 1px
of next cell. for do that I have to implement a manual paging for have a
good position of every cell, and i make this code for the
MainConllectionView:
//Start DRAGGING - SET POINT FIRST TOUCH
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
CGFloat pageWidth = self.collectionView.frame.size.width + 10;
_currentPage = floor((self.collectionView.contentOffset.x - pageWidth
/ 2) / pageWidth) + 1;
NSLog(@"Dragging - You are now on page %i", _currentPage);
}
//END DRAGGING - CALCULATE THE VELOCY FOR PAGING
-(void) scrollViewWillEndDragging:(UIScrollView*)scrollView
withVelocity:(CGPoint)velocity targetContentOffset:(inout
CGPoint*)targetContentOffset {
CGFloat pageWidth = self.collectionView.frame.size.width + 10;
newPage = _currentPage;
if (velocity.x == 0) // slow dragging not lifting finger
{
newPage = floor((targetContentOffset->x - pageWidth / 2) /
pageWidth) + 1;
}
else
{
newPage = velocity.x > 0 ? _currentPage + 1 : _currentPage - 1;
if (newPage < 0)
newPage = 0;
if (newPage > self.collectionView.contentSize.width / pageWidth)
newPage = ceil(self.collectionView.contentSize.width /
pageWidth) - 1.0;
}
NSLog(@"Dragging - You will be on %i page (from page %i)", newPage,
_currentPage);
*targetContentOffset = CGPointMake(newPage * pageWidth,
targetContentOffset->y);
[self.collectionView setUserInteractionEnabled:NO];
}
//START TO DECELERAT - FIX THE CONTENTOFSET FOR CORRECT POSITION
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
int cellDifference = 2;
int leftSpanningScroll = cellDifference/2;
CGFloat widhtSV = self.collectionView.frame.size.width;
[self.collectionView setContentOffset:
CGPointMake(widhtSV*(newPage) - (newPage
*cellDifference)-leftSpanningScroll, 0) animated:YES];
}
//SCROLLING IS ENDED THE USER INTERACTION IS ENABLED
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self.collectionView setUserInteractionEnabled:YES];
}
And this code for the SubCollectionView Paging:
//Start DRAGGING - SET POINT FIRST TOUCH
- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {
CGFloat pageHeight = self.collectionView.frame.size.height + 10;
_currentPage = floor((self.collectionView.contentOffset.y - pageHeight
/ 2) / pageHeight) + 1;
NSLog(@"Dragging - You are now on page %i", _currentPage);
}
//END DRAGGING - CALCULATE THE VELOCY FOR PAGING
-(void) scrollViewWillEndDragging:(UIScrollView*)scrollView
withVelocity:(CGPoint)velocity targetContentOffset:(inout
CGPoint*)targetContentOffset {
CGFloat pageHeight = self.collectionView.frame.size.height + 10;
newPage = _currentPage;
if (velocity.y == 0) // slow dragging not lifting finger
{
newPage = floor((targetContentOffset->y - pageHeight / 2) /
pageHeight) + 1;
}
else
{
newPage = velocity.y > 0 ? _currentPage + 1 : _currentPage - 1;
if (newPage < 0)
newPage = 0;
if (newPage > self.collectionView.contentSize.height/ pageHeight)
newPage = ceil(self.collectionView.contentSize.height /
pageHeight) - 1.0;
}
NSLog(@"Dragging - You will be on %i page (from page %i)", newPage,
_currentPage);
*targetContentOffset = CGPointMake(targetContentOffset->x, newPage *
pageHeight);
[self.collectionView setUserInteractionEnabled:NO];
}
//START TO DECELERAT - FIX THE CONTENTOFSET FOR CORRECT POSITION
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
int cellDifference = 2;
int leftSpanningScroll = cellDifference/2;
CGFloat heightSV = self.collectionView.frame.size.height;
[self.collectionView setContentOffset:
CGPointMake(0, heightSV*(newPage) - (newPage
*cellDifference)-leftSpanningScroll) animated:YES];
}
//SCROLLING IS ENDED THE USER INTERACTION IS ENABLED
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[self.collectionView setUserInteractionEnabled:YES];
}
My problem is when load the first cell of MainCollectionView everything is
works, but if I go to the next cell of MainCollectionView and I come back
to the first, the paging by up and down swipe doesn't work, i have this
problem just in the first and last cell of mainControllerView the other
cell works every time. if I do the same for the scroll up and down andI
have the same problem works in every cells but no in the first and last,
when i come back to the main cell it stop to work.
the toggle of paging in the storyboard is OFF for both CollectionView i
try also to leave the [self.collectionView setUserInteractionEnabled:YES];
and [self.collectionView setUserInteractionEnabled:NO]; and i have the
same problem. I saw that when I come back in the first cell of
MainCollectionView and i try to scroll up and down the app don't see it
and go in the code for scroll left and right of the MainControllerView.
Thx
Mirko

No comments:

Post a Comment