Objective-C:没有 pagingEnabled 的无尽 UIScrollView

发布于 2024-08-30 21:53:02 字数 147 浏览 10 评论 0原文

在我的 iPhone 应用程序中,有一个滚动视图 pagingEnabled=NO ,它最多可以包含 200 个子视图 (150 x 150),挑战是进行延迟加载并模拟水平方向上的无限滚动(无弹跳)。

对于这个请求有解决方案或替代方案吗?

In my iPhone app there is a scrollview pagingEnabled=NO which can contain up to 200 subviews (150 x 150) and the challenge is to do lazy loading and simulate endless scrolling (without bouncing) in horizontal directions.

Is there a solution or an alternative for this request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自控 2024-09-06 21:53:02

Apple 的示例代码项目之一演示了滚动视图的延迟加载: PageControl

为了伪造无休止的滚动,我的建议是让你的滚动视图一开始就非常宽,比普通人在一组滚动行为中滚动的宽度要宽。然后在您的委托方法中 -scrollViewDidEndScrollingAnimation:-scrollViewDidEndDragging:willDecelerate:-scrollViewDidEndDecelerating: 中,其中一个或多个将在用户完成滚动后,将您的内容重新定位到滚动视图的中心,并更新您的 contentOffset 点而不使用动画。

为了使其在视觉上工作,您还需要禁用水平滚动条。您还需要考虑如何使用此方法确定在特定 contentOffset 处绘制哪个视图,因为您无法仅将 contentOffset.x 除以不再滚动视图的边界来找出您所在的位置。

Lazy loading of a scroll view is demonstrated in one of Apple's sample code projects: PageControl.

To fake endless scrolling what I'd suggest is to make your scroll view very wide to begin with, wider than an average person would scroll in one set of scrolling behaviors. Then in your delegate methods -scrollViewDidEndScrollingAnimation:, -scrollViewDidEndDragging:willDecelerate: and -scrollViewDidEndDecelerating:, one or more of which will be called after the user is done scrolling, reposition your content to exist in the center of the scroll view and update your contentOffset point without animating.

For that to work visually you would need to also disable the horizontal scroller. You'll also need to consider how to determine what view to draw at a particular contentOffset with this method since you won't be able to just divide the contentOffset.x by the scroll view's bounds anymore to find out where you are.

若言繁花未落 2024-09-06 21:53:02

你好,我找到了方法。
我有一个包含所有子视图的主数组(在我的例子中它们是图像,所以我存储名称)。
滚动视图只有 3 个子视图:左视图、当前视图、右视图。
分页已启用,因此用户在任何时候都无法向左/向右旋转多个视图。
我所做的是:
1) 跟踪他在主阵列上的当前位置。如果他向左移动,则减一;右加一。像这样:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  [some code to determine current page, based on contentOffset]
    if (page == 0){
        NSLog(@"Going left");
        if (currentPage > 0){
            currentPage--;
        } else {
            //cycle to last
            currentPage = [images count] -1;
        }
    } else if (page == 2){
        NSLog(@"Going right");
        if (currentPage < ([images count] -1)){
            currentPage++;
        } else {
            //cycle to first
            currentPage = 0;
        }
    } else{
        NSLog(@"Not moving");
    }

2)用户移动后,我重新加载3个新图像,像这样:

//updates the 3 views of the scrollview with a new center page.
-(void) updateScrollViewForIndex:(NSInteger)newCenterPage{
    //fist clean scroll view
    for (UIView *sView in [scroll subviews]){
        [sView removeFromSuperview];
    }

    NSInteger imgCount = [images count];

    //set center view
    [self loadImageIndex:newCenterPage atPosition:1];

    //set left view
    if (newCenterPage > 0){
        [self loadImageIndex:newCenterPage-1 atPosition:0];

    } else {
        //its the first image, so the left one is the last one
        [self loadImageIndex:imgCount-1 atPosition:0];
    }

    //set right view
    if (newCenterPage < imgCount-1){
        [self loadImageIndex:newCenterPage+1 atPosition:2];
    } else {
        //its the last image, so ther right one is the first one
        [self loadImageIndex:0 atPosition:2];       
    }
}

3)最后再次将滚动视图重新居中到中心视图:

[scroll setContentOffset:CGPointMake(1 * viewWidth, 0)];

希望这会有所帮助,尽管“有计划的人”是克拉克先生,谁指路了。

贡索

Hello I found the way to do it.
I have a master array with all the subviews (in my case they are images, so I store the names).
The scrollview only has 3 subviews: left, current, right.
Paging is enabled, so the user cant really spin more that one view left/right at any time.
What I do is:
1) track his current position on the master array. If he moves left, subtract one; right add one. Like this:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  [some code to determine current page, based on contentOffset]
    if (page == 0){
        NSLog(@"Going left");
        if (currentPage > 0){
            currentPage--;
        } else {
            //cycle to last
            currentPage = [images count] -1;
        }
    } else if (page == 2){
        NSLog(@"Going right");
        if (currentPage < ([images count] -1)){
            currentPage++;
        } else {
            //cycle to first
            currentPage = 0;
        }
    } else{
        NSLog(@"Not moving");
    }

2) after the user moves, I reload 3 new images, like this:

//updates the 3 views of the scrollview with a new center page.
-(void) updateScrollViewForIndex:(NSInteger)newCenterPage{
    //fist clean scroll view
    for (UIView *sView in [scroll subviews]){
        [sView removeFromSuperview];
    }

    NSInteger imgCount = [images count];

    //set center view
    [self loadImageIndex:newCenterPage atPosition:1];

    //set left view
    if (newCenterPage > 0){
        [self loadImageIndex:newCenterPage-1 atPosition:0];

    } else {
        //its the first image, so the left one is the last one
        [self loadImageIndex:imgCount-1 atPosition:0];
    }

    //set right view
    if (newCenterPage < imgCount-1){
        [self loadImageIndex:newCenterPage+1 atPosition:2];
    } else {
        //its the last image, so ther right one is the first one
        [self loadImageIndex:0 atPosition:2];       
    }
}

3) Finally re-center the scroll view to the center view again:

[scroll setContentOffset:CGPointMake(1 * viewWidth, 0)];

Hope this helps, although "the man with the plan" is Mr. Clark, who pointed the way.

Gonso

隔纱相望 2024-09-06 21:53:02

Matt Gallagher 有一篇博客文章,其中描述了此问题的解决方案确切的问题。我用过它,效果很好。

Cocoa Touch 中的 UIScrollView 和 UIPageControl 允许具有多个平移页面的用户界面。 Apple 提供的示例项目 (PageControl) 将每个页面的所有子视图保存在延迟加载的数组中。我将向您展示如何仅使用两个子视图来实现此目的,无论您希望表示多少个虚拟页面。

它的工作原理是在子视图周围打乱并在必要时重置其内容。我用它来显示闪存卡,其中可能有 3 到 3,000 个项目。尽管它现在已设置为分页,但我相信您可以让它与常规滚动一起使用。

Matt Gallagher has a blog post which describes a solution to this exact problem. I've used it and it works great.

The UIScrollView and UIPageControl in Cocoa Touch allow for user interfaces with multiple panning pages. The sample project that Apple provides (PageControl) keeps all child views for every page in a lazily loaded array. I'll show you how you can implement this using just two child views, no matter how many virtual pages you wish to represent.

It works by shuffling around the child views and reseting their content when necessary. I used this for displaying flash cards, where there could be anywhere from 3 to 3,000 items. Although it's set up right now for paging, I'm sure you could get it to work with regular scrolling.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文