UICollectionView: auto scroll view for swap

I am trying to scroll UICollectionViewwhich is off screen in my application, under the code below.

int pages = ceil(aCollectionView.contentSize.height / aCollectionView.frame.size.height);

for (int i = 0; i < pages; i ++)
{
     NSArray *sortedVisibleItems = [[aCollectionView indexPathsForVisibleItems] sortedArrayUsingSelector:@selector(compare:)];

     NSIndexPath *lastItem = [sortedVisibleItems lastObject];

     // 2.next position
     NSInteger nextItem = lastItem.item + 1;
     NSInteger nextSection = lastItem.section;
     NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];

     [self takeImage];

     dispatch_async(dispatch_get_main_queue(), ^
    {            
        [aCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
    });
}

And take screenshots of each page to print. But this is not scrolling and always prints the 1st page several times.

UICollectionView Property

enter image description here

Not enough or doing in the wrong direction?

0
source share
3 answers

You always get the last object NSIndexPath *lastItem = [sortedVisibleItems lastObject]; to take a photo. This will always capture only one page.

This is because you are not removing lastObject from your array.

Delete your last object with

[sortedVisibleItems removeLastObject];
0
source
int pages = ceil(aCollectionView.contentSize.height / aCollectionView.frame.size.height);
NSArray *visibleItems = [aCollectionView indexPathsForVisibleItems];
NSInteger row = 0;
NSIndexPath *currentItem;
for (NSIndexPath *indexPath in visibleItems) {
    if (row < indexPath.row){
        row = indexPath.row;
        currentItem = indexPath;
    }
}
NSLog(@"current indexpath ; %ld",(long)currentItem.row);
if (currentItem.row == pages-1) {
    return;
}
NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
[aCollectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES];

try it

0

. , .

. UIScrollView, .

, ... , , : https://github.com/sgr-ksmt/PDFGenerator

, , , , , .

0

Source: https://habr.com/ru/post/1624747/


All Articles