如何实现具有 1000 多个子视图的 UIScrollView?

How to implement UIScrollView with 1000+ subviews?(如何实现具有 1000 多个子视图的 UIScrollView?)
本文介绍了如何实现具有 1000 多个子视图的 UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在努力编写应用程序的一部分,该应用程序的行为应该类似于原生 iphone 照片应用程序.查看了 Orielly 的 iphone sdk 应用程序开发书,其中提供了实现这种所谓的页面滑动的示例代码.那里的代码首先创建了所有子视图,然后隐藏/取消隐藏它们.在给定时间,只有 3 个子视图可见,其余的被隐藏.经过一番努力,我让它与当时只有大约 15 页的应用程序一起工作.

I am struggling with writing portion of an app which should behave like the native iphone photo app. Looked at iphone sdk app development book from Orielly which gave an example code for implementing this so-called page-flicking. The code there first created all subviews and then hide/unhide them. At a given time only 3 subviews are visible rest are hidden. After much effort I got it working with app which at that time had only around 15 pages.

当我添加 300 页时,很明显这种预先分配这么多子视图的方法存在性能/内存问题.然后我想可能就我的情况而言,我应该只分配 3 个子视图,而不是隐藏/取消隐藏它们.可能我应该在运行时删除/添加子视图.但是不知道 UIScrollView 是否可以动态更新内容.例如,如 UIScrollView 所理解的,一开始有 3 个帧位于屏幕的不同 x 偏移量(0、320、640).一旦用户移动到第 3 页,我如何确保我能够添加第 4 页并删除第 1 页,但 UIScrollView 不会混淆?

As soon as I added 300 pages, it became clear that there are performance/memory issues with that approach of pre-allocating so many subviews. Then I thought may be for my case I should just allocate 3 subviews and instead of hide/unhide them. May be I should just remove/add subviews at runtime. But can't figure out whether UIScrollView can dynamically update contents. For example, at the start there are 3 frames at different x-offsets ( 0, 320, 640 ) from the screen as understood by UIScrollView. Once user moves to 3rd page how do I make sure I am able to add 4th page and remove 1st page and yet UIScrollView doesn't get confused ?

希望有针对此类问题的标准解决方案...有人可以指导吗?

Hoping there is a standard solution to this kind of problem...can someone guide ?

推荐答案

UIScrollView 只是 UIView 的一个子类,因此可以在运行时添加和删除子视图.假设您有固定宽度的照片(320 像素)并且有 300 张,那么您的主视图将是 300 * 320 像素宽.创建滚动视图时,将框架初始化为那么宽.

UIScrollView is just a subclass of UIView so it's possible to add and remove subviews at runtime. Assuming you have fixed width photos (320px) and there are 300 of them, then your main view would be 300 * 320 pixels wide. When creating the scroll view, initialize the frame to be that wide.

因此,滚动视图的框架将具有 (0, 0) 到 (96000, 480) 的尺寸.每当您添加子视图时,您都必须更改它的框架,使其适合其父视图中的正确位置.

So the scroll view's frame would have the dimensions (0, 0) to (96000, 480). Whenever you are adding a subview, you will have to change it's frame so it fits in the correct position in its parent view.

假设我们将第四张照片添加到滚动视图中.它的帧将从 (960, 480) 到 (1280, 480).如果您可以以某种方式将索引与每张图片相关联,那么这很容易计算.然后用它来计算索引从0开始的图片帧:

So let's say, we are adding the 4th photo to the scroll view. It's frame would be from (960, 480) to (1280, 480). That is easily to calculate, if you can somehow associate an index with each picture. Then use this to calculate the picture's frame where indexes start at 0:

Top-Left -- (320 * (index - 1), 0)

Bottom-Right -- (320 * index, 480)

删除第一张图片/子视图应该很容易.保留当前屏幕上的 3 个子视图的数组.每当你在屏幕上添加一个新的子视图时,也要将它添加到这个数组的末尾,然后从屏幕上删除这个数组中的第一个子视图.

Removing the first picture/subview should be easy. Keep an array of the 3 subviews currently on-screen. Whenever you are adding a new subview to the screen, also add it to the end of this array, and then remove the first subview in this array from the screen too.

这篇关于如何实现具有 1000 多个子视图的 UIScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
How to Cancel Scrolling in UIScrollView(如何在 UIScrollView 中取消滚动)
How can i add a activity indicator below the UITableView?(如何在 UITableView 下方添加活动指示器?)
NSURLRequest won#39;t fire while UIScrollView is scrolling(NSURLRequest 在 UIScrollView 滚动时不会触发)