如何使用1000多个子视图实现UIScrollView? [英] How to implement UIScrollView with 1000+ subviews?

查看:124
本文介绍了如何使用1000多个子视图实现UIScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力编写一个应该像原生iphone照片应用程序一样的应用程序部分。看看Orielly的iphone sdk app开发书,它给出了实现这个所谓的页面轻弹的示例代码。那里的代码首先创建所有子视图,然后隐藏/取消隐藏它们。在给定时间,只有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可以理解,屏幕上有不同x偏移(0,320,640)的3帧。一旦用户移动到第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.

所以,假设我们正在添加<滚动视图中的strong> 4th 照片。它的帧是从(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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆