如何制作无限的UIScrollView?

Jay*_*hta 5 iphone uiscrollview ios

我使用此代码来重用视图UIScrollView,并且运行良好.知道如何使用重用子视图使这段代码无限滚动UIScrollView吗?

#pragma mark 
#pragma mark - SCROLL VIEW DELEGATE METHOD
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
             [self setNeedsLayout];

            int currentPosition = floor(scrollView.contentOffset.x);
            currentPage = MAX(0,floor(currentPosition / scrollView.frame.size.width));

            if(currentPage != refPage)
            {
                refPage = currentPage;
                if ([arrQLst count] > 0)
                {
                    [self replaceHiddenView];
                }
            }
        }
           - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
        {
            if ([arrQLst count] > 0)
            {
                [self replaceHiddenView];
            }
        }
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 -(void)replaceHiddenView
    {
        const double pageWidth =self.frame.size.width;
        NSInteger currentIndex;

        currentIndex = (([self getScrollView].contentOffset.x - pageWidth) / pageWidth) + 1;

        RandomView *currentView = nil;
        RandomView *previousView = nil;
        RandomView *nextView = nil;

        if (CGRectContainsPoint(objRandom1.frame, [self getScrollView].contentOffset))
        {
            currentView = objRandom1;
            previousView = objRandom2;
            nextView = objRandom3;
        }
        else if (CGRectContainsPoint(objRandom2.frame, [self getScrollView].contentOffset))
        {
            currentView = objRandom2;
            previousView = objRandom1;
            nextView = objRandom3;
        }
        else
        {
            currentView = objRandom3;
            previousView = objRandom1;
            nextView = objRandom2;
        }

        currentView.frame = CGRectMake(self.frame.size.width * currentIndex, 0.0, self.frame.size.width, self.frame.size.height);
        [currentView setQuestionData:[arrQLst objectAtIndex:currentIndex]];

        // Now move the other ones around
        // and set them ready for the next scroll
        if (currentIndex < [arrQLst count] - 1)
        {
            nextView.frame = CGRectMake(self.frame.size.width * (currentIndex + 1), 0.0, self.frame.size.width, self.frame.size.height);
            [nextView setQuestionData:[arrQLst objectAtIndex:(currentIndex + 1)]];
        }

        if (currentIndex >= 1)
        {
            previousView.frame = CGRectMake(self.frame.size.width * (currentIndex - 1), 0.0, self.frame.size.width, self.frame.size.height);
            [previousView setQuestionData:[arrQLst objectAtIndex:(currentIndex - 1)]];
        }

        if([arrQLst count] > 0)
            [self checkForRequestForLoadMore:currentIndex];
     }

    -(void)displayViewAtIndex:(NSInteger)index
    {
        @try
        {
            NSInteger count = [arrQLst count];
            if (index >= 0 && index < count)
            {
                Ques *objQ = [arrQLst objectAtIndex:index];
                objRandom1.frame = CGRectMake(self.frame.size.width * index, 0.0,self.frame.size.width,self.frame.size.height);
                [objRandom1 setQuestionData:objQ];
                [[self getScrollView] scrollRectToVisible:CGRectMake(self.frame.size.width * index, 0.0, self.frame.size.width, self.frame.size.height) animated:NO];

                if (index < (count - 1))
                {
                    objQ = [arrQLst objectAtIndex:(index + 1)];
                    objRandom2.frame = CGRectMake(self.frame.size.width * (index + 1), 0.0, self.frame.size.width, self.frame.size.height);
                    [objRandom2 setQuestionData:objQ];
                }

                if (index > 0)
                {
                    objQ = [arrQLst objectAtIndex:(index - 1)];
                    objRandom3.frame = CGRectMake(self.frame.size.width * (index - 1), 0.0, self.frame.size.width, self.frame.size.height);
                    [objRandom3 setQuestionData:objQ];
                }
            }
        }
        @catch (NSException *exception)
        {
            NSLog(@"displayViewAtIndex Exception %s",__PRETTY_FUNCTION__);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Jay*_*hta 2

最后找到了解决方案并在上面的代码中创建无限滚动视图。这里我们使用与上面相同的代码来为视图设置框架。

代码:

//在这里,我在数组的第一个和最后一个索引处添加了两个虚拟数据,在第一个位置添加了最后一个对象,在最后一个对象中添加了arrData的第一个对象。当当时设置数据时,我们创建第一个对象的两个虚拟数据和最后一个对象。

        if ([arrQLst count] > 1)
        {
            [arrQLst insertObject:[arrQLst lastObject] atIndex:0];
            [arrQLst addObject:[arrQLst objectAtIndex:1]];
        }
        //end
        [self setContentSizeOfScrollView];
Run Code Online (Sandbox Code Playgroud)

-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    [self setNeedsLayout];

    int currentPosition = floor(scrollView.contentOffset.x);
    currentPage = MAX(0,floor(currentPosition / scrollView.frame.size.width));

    if(currentPage != refPage)
    {
        refPage = currentPage;
        if ([arrQLst count] > 0)
        {
            [self replaceHiddenView];
        }
    }


   if (scrollView.contentOffset.x <= 0.0)
    {
        if ([arrQLst count] > 1)
        {
            [self displayViewAtIndex:([arrQLst count]-2)];
        }
    }
    else if (scrollView.contentOffset.x > (scrollView.frame.size.width * ([arrQLst count] - 1)))
    {
        if ([arrQLst count] > 1)
        {
            [self displayViewAtIndex:1];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)