【问题标题】:UICollectionView : Offscreen auto scroll for pagingUICollectionView : 屏幕外自动滚动以进行分页
【发布时间】:2017-08-14 11:23:50
【问题描述】:

我正在尝试通过以下代码滚动我的应用程序中屏幕外的UICollectionView

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];
    });
}

并为打印目的拍摄每一页的屏幕截图。 但它不滚动并且总是多次打印第一页。

UICollectionView 的属性

我错过了还是做错了方向?

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    你总是得到NSIndexPath *lastItem = [sortedVisibleItems lastObject]的最后一个对象;拍照。这只会捕获相同的页面。

    这是因为您没有从数组中删除 lastObject。

    使用删除你的lastObject

    [sortedVisibleItems removeLastObject];
    

    【讨论】:

    • 不,该数组用于可见索引。所以最后一个对象表示页面中最后一个可见索引,所以我可以添加 1 并移动到下一页的下一个单元格。
    【解决方案2】:
    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];
    

    试试这个

    【讨论】:

    • 你遇到了什么问题?
    【解决方案3】:

    集合视图仅在主线程上更新。你的代码被包裹在一个循环中,它永远不允许主线程运行和更新。

    有很多关于这个的讨论。许多与使用UIScrollView 做同样的事情直接相关,但这是同一个问题。

    您可能想看看这个...不确定它是否符合您的需求,但我已经多次看到它被引用:https://github.com/sgr-ksmt/PDFGenerator

    如果这对您不起作用,那么它可能包含您需要的技术,因此对该代码进行一些调查应该会找到答案。

    【讨论】:

    • 我也试过`dispatch_async(dispatch_get_main_queue(), ^ { [aCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO]; }); ` 检查我更新的问题
    • 再一次,这行不通,因为您阻塞了循环中的主线程。所以现在,每次通过你告诉 iOS:“滚动集合视图,但在你做之前等待主线程......但是继续[self takeImage] now”。这是给你的另一个链接 - 这个是 Obj-C,更直接(更少的选项):github.com/azu/ScrollViewToPDF
    猜你喜欢
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多