iphone - 如何在垂直 UIScrollView 中延迟加载 UIView

标签 iphone uiview uiscrollview lazy-loading

我有一个垂直的 UIScrollView,其中包含多个缩略图(UIView)。在我的 viewWillAppear 中,我用 200 个缩略图填充 UIScrollView。

滚动并表示此 View 并不需要很长时间,但加载整个缩略图 View 却需要很长时间。因此,我正在寻找一种方法,可以在 viewWillAppear 上显示前 20 个缩略图,并将其余缩略图加载到 UIScrollView -(void)scrollViewDidScroll:(UIScrollView *)scrollView{.

但是实际的实现对我来说并不是很清楚。我是预加载 viewWillAppear 上的所有缩略图还是仅预加载前 20 个?如何跟踪需要在scrollViewDidScroll上绘制的新缩略图的位置?希望有人能指出我正确的方向。

这是我当前 viewWillAppear 的实现,它绘制了我的缩略图

for (int i = 0; i <= 20; i++){
            birdRow = (int) floor(i / 4);
            birdColumn = (i % 4) + 1; 
            paddingX = 125;
            UIView * thumbnail = [[UIView alloc] initWithFrame:CGRectMake(170*birdColumn - paddingX, 180*birdRow + 20, 160, 170)];
            [scroller addSubview:thumbnail];
}

最佳答案

我所做的与您想要的有所不同,或者与您想要的有所不同,如果我明白的话,请进行一些修改,只需查看下面的代码

用于存储 ScrollView 的最后位置

float lastContentOffset;

还使用一个跟踪数据索引来加载后记所需的数据

NSInteger index;

第一次感觉scrollView时,我将值分配给index和lastContentOffset

index = Currently shown image index;
lastContentOffset = scrollview.contentOffset.x;//it shows currently visible rect's origin's x value

现在在 ScrollView 的委托(delegate)方法中scrollViewDidEndDecelerating:我用来更改如下值

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"ScrollViewDidScroll is called"); 
    if(lastContentOffset < scrollView.contentOffset.x)
    {
        index += (int)(scrollview.contentOffset.x - lastContentOffset)/scrollview.frame.size.width;
        NSLog(@"Index incremented %d",index);
        //Load required new images also can remove some of the images
    }
    else if(lastContentOffset > scrollView.contentOffset.x)
    {
        index -= (int)(lastContentOffset - scrollview.contentOffset.x)/scrollview.frame.size.width;
        NSLog(@"Index decremented %d",index);
        //Load required new images also can remove some of the images
    }
    lastContentOffset = scrollView.contentOffset.x;
    NSLog(@"New lastContentOffset %.2f",lastContentOffset);
}

关于iphone - 如何在垂直 UIScrollView 中延迟加载 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11097004/

相关文章:

iphone - 制作 Activator 监听器时遇到问题(使用 theos)

iphone - 如何将 UIView 移到其 SuperView 之外?

ios - 使用 PageViewController 来处理大量页面,例如相机胶卷 ImageView ?

ios - UIViewController中有两个scrollView时如何调用UIScrollView的delegate方法

iOS - UIScrollView 中的 UITableView - 滑动以显示其他内容

iphone - UIView 截断而不是拉伸(stretch)

ios - iPhone 应用程序在设备中崩溃,但它在模拟器中运行

ios - 我应该为应用程序中的每个表创建一个 UITableViewCell 类吗?

c# - 图像不保留缩放比例和偏移

iphone - UIScrollView 初始化为 {0, 0} 处的框架,但宽度和高度为 {0, 0}