UIScrollView分页加载子视图比滚动更慢,如何取消手势,延迟,...?

时间:2013-03-20 16:16:15

标签: ios uiscrollview gesture scroll-paging

当快速滚动滚动视图时,我不确定内容是否加载得不够快或者是否跳过了绘图,因为我通过检查scrollViewDidEndDecelerating来生成新的子视图:(有更好的方法吗?)不需要快速滚动! - 我只是想解决它引起的问题。

更长的解释: 我有一个内容大小和分页启用的scrollview。对于给定页面,添加三个子视图,其中帧原点为-1,0,+ 1倍帧大小(因此中间帧可见,其他帧是下一页和上一页)。帧-1和+1是ImageViews,帧0是另一个具有自己的ImageView子视图的UIScrollView。它们不是子类。

出于性能原因,我的方法是检查滚动是向前还是向后移动,并且只加载新图像,因为已经加载了另外两个图像,我将它们存储起来,例如, “UIImage * tempImg1 = previousPageImageView.image”并在添加了子视图后从那里获取它们。

现在快速滚动会导致新图像不显示,但更糟糕的是,程序会对它应该处于哪个索引并且处于混乱状态感到困惑。我用小图片尝试过但同样的问题。

不一定允许用户快速滚动,在启动页面移动之后忽略进一步的滑动手势可能就足够了,直到加载完成为止。

非常感谢您的想法。

页面加载:

-(void)loadSVforPage:(int)page
{
    int nextSVpage = page +1;
    int previousSVpage = page -1;
    int numberOfAssets = assets.count;

    ALAsset *displayAsset;
    ALAssetRepresentation *assetRep;
    CGFloat SVwidth = scrollLarge.frame.size.width;
    CGFloat SVheight = scrollLarge.frame.size.height;

    UIImage *tempImg1;
    UIImage *tempImg2;
    UIImage *tempImg3;
    UIImage *displayed;


    if (page != currentpage)
    {

        // if moves are +1 or -1 use temp images, else load all
        if (page == currentpage +1) // moving forward
        {
            //NSLog(@"page+");
            tempImg1 = nextSVImg;   // the former "next", which now becomes current
            tempImg3 = currentSVImg;  // the former "current", which now becomes previous

            if ((page+1 >=0) && (page+1 <= assets.count-1))  
            {
                displayAsset = [assets objectAtIndex:page+1];  // the "new next" image 
                assetRep = [displayAsset defaultRepresentation];
                tempImg2 = [ImageProcessing fullSizeImageForAssetRepresentation:assetRep];
            }
        }
        else if (page == currentpage -1) // moving backwards

        ....
        ....
        ....

        // LOAD PAGE+1 

        if (nextSVpage <= numberOfAssets-1)
        {
            displayed = tempImg2;

                nextSVImg = displayed;  // store the image for future scrolls

                CGRect frame = CGRectMake(nextSVpage * SVwidth, 0, SVwidth, SVheight);

                //old code adding UIImageViews
                //------------------------------
                //nextScrollImageView = [[UIImageView alloc]initWithFrame:frame];
                //[completeScrollableImageView addSubview:nextScrollImageView];
                //nextScrollImageView.image = displayed;

                // new code with subclassed UIView and image in drawRect
                //-------------------------------
                scrollUIViewNext = [[scrollviewImage alloc]initWithFrame:frame andImage:displayed];
                [completeScrollableImageView addSubview:scrollUIViewNext];

                ....
                ....
                ....

触发加载:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == 0)  // the base SV
    {
        int pageNumber = floor(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);//+1;
        if (pageNumber != currentpage)
        {
            //NSLog(@"moved from current %i to %i",currentpage,pageNumber);
            [self loadSVforPage:pageNumber];
        }
    } 
}

0 个答案:

没有答案