1

我有一个关于启用分页的 UIScrollView 的问题,其中包含许多 UIView,每个都由自己的 UIViewController 管理。

现在有大约 20 到 30 个 UIViewControllers 可以包含在 UIScrollView 中。它是 iPad 上的一个目录应用程序,我一开始就开始预加载所有视图,但随着 UIViewController 的数量越来越大,这不再是一种选择。

我正在寻找内存使用方面的完美解决方案。当 ScrollView 的 ContentOffset 到达特定控制器时,重新加载 UIViewControllers 是没有问题的。而且我认为当 ContentOffset 告诉我不再需要 UIViewControllers 时将 UIViewControllers 归零也不是那么难。

处理这个问题的正确方法是什么?在需要时分配 UIViewController 是否足够,将它们放入 NSMutableDictionary 或 NSMutableArray 并在不再需要它们时将它们归零?从已经做过类似事情的人那里得到一点帮助会很棒!

谢谢你的帮助!

4

1 回答 1

5

我敢肯定那里有一些很好的无限滚动类,但如果你要“自己动手”,这里有一段极简的代码,它演示了无限滚动的过程,保持当前、上一页和下一页在记忆中,但放开其他任何东西。这假设:

  • 您正在进行水平滚动并已打开分页;
  • 您正在为子视图使用视图控制器;
  • 您的子视图控制器类有一个page属性来跟踪它的页面;和
  • 您已将视图控制器设置为滚动视图的委托

因此,它可能看起来像:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // my underlying model is just an array of strings, which I'll show on my child
    // view; your model will be more elaborate, but I just want to illustrate the concept

    self.objects = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9"];

    // set the `contentSize` for the scrollview

    CGRect content = self.view.bounds;
    content.size.width *= [self.objects count]; // make it wide enough to hold everything
    self.scrollView.contentSize = content.size;

    // set our current page and load the first pages (the first and the next pages)

    self.currentPage = 0;
    [self addChildPage:0 toScrollView:self.scrollView];
    [self addChildPage:1 toScrollView:self.scrollView];
}

- (void)addChildPage:(NSInteger)page toScrollView:(UIScrollView *)scrollView
{
    // create the child controller

    ChildViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"child"];

    // set whatever properties you need to in order for it to present its information correctly

    controller.text = self.objects[page];
    controller.page = page;

    // now do the stuff to add it to the right place in the scrollview

    CGRect frame = self.view.bounds;
    frame.origin.x = frame.size.width * page;
    controller.view.frame = frame;
    [self addChildViewController:controller];        // containment call for adding child view controller
    [scrollView addSubview:controller.view];
    [controller didMoveToParentViewController:self]; // containment call when done adding child
}

- (ChildViewController *)childControllerForPage:(NSInteger)page
{
    for (ChildViewController *controller in self.childViewControllers)
    {
        if (controller.page == page)
            return controller;
    }

    return nil;
}

- (void)addChildIfNecessary:(NSInteger)page toScrollView:(UIScrollView *)scrollView
{
    if (page < 0 || page >= [self.objects count])
        return;

    ChildViewController *controller = [self childControllerForPage:page];

    if (controller == nil)
        [self addChildPage:page toScrollView:scrollView];
}

- (void)removeChildController:(UIViewController *)controller
{
    [controller willMoveToParentViewController:nil];  // containment call before removing child
    [controller.view removeFromSuperview];
    [controller removeFromParentViewController];      // containment call to remove child
}

- (void)updateChildrenViewsForPage:(NSInteger)page forScrollView:(UIScrollView *)scrollView
{
    if (page == self.currentPage)
        return;

    // add child pages as necessary

    [self addChildIfNecessary:page     toScrollView:scrollView];
    [self addChildIfNecessary:(page-1) toScrollView:scrollView];
    [self addChildIfNecessary:(page+1) toScrollView:scrollView];

    // find any pages that need removing

    NSMutableArray *pagesToRemove = [NSMutableArray array];
    for (ChildViewController *controller in self.childViewControllers)
    {
        if (controller.page < (page - 1) ||
            controller.page > (page + 1))
        {
            [pagesToRemove addObject:controller];
        }
    }

    // remove them if they need removing

    for (UIViewController *controller in pagesToRemove)
    {
        [self removeChildController:controller];
    }

    // update our "current page" index

    self.currentPage = page;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width + 0.5;

    [self updateChildrenViewsForPage:page forScrollView:scrollView];
}

这演示了适当的自定义容器调用和滚动事件的处理。我希望这有帮助。

于 2013-03-25T19:31:20.437 回答