【问题标题】:How to make infinite UIScrollView?如何制作无限的 UIScrollView?
【发布时间】:2015-02-23 06:18:55
【问题描述】:

我使用this codeUIScrollView 中创建重用视图,效果很好。知道如何使用 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__);
        }
    }

【问题讨论】:

  • 请在您的问题中发布您现有的代码(通过使用“编辑”功能)
  • 嗨@Raptor 我已经编辑了我的问题,请检查代码。在这段代码中,当时调用了 didScroll 方法时调用了 replaceHiddenView 方法。

标签: ios iphone uiscrollview


【解决方案1】:

这是制作无限滚动视图的方法。此应用程序使用您必须从终端下载的可可豆荚。 Github 有很多很棒的开源框架来跟上你的想法。 https://github.com/pronebird/UIScrollView-InfiniteScroll

【讨论】:

  • 虽然引用的 Github 资源对 OP 非常有用,但最好发表评论,或者为 OP 做一个简短的代码演示。
  • 你好@Program1 谢谢你给出的答案,但我需要在我的代码上方无限滚动。
【解决方案2】:

终于找到解决方案,在上面的代码中创建无限滚动视图。这里我们使用上面相同的代码来设置视图的框架。

代码:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 2013-10-16
    • 2010-11-14
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多