简体   繁体   English

UIScrollView:当用户滚动时,如何在可滚动网格中回收对象?

[英]UIScrollView: How would I recycle objects in my scrollable grid when the user scrolls?

I've got an UIScrollView with a grid of images. 我有一个带有图像网格的UIScrollView。 Currently they're only 6, but there might be a few hundred some day. 目前他们只有6岁,但可能会有几百天。 So of course it would be a bad idea to create a few hundred UIImageView objects inside that UIScrollView and fill them all up with image data. 因此,在UIScrollView内创建数百个UIImageView对象并用图像数据填充所有对象将是一个坏主意。 I think I was reading this somewhere in the Apple docs: They recommend to only create so mutch elemends as you need to fill up the scrollable visible area. 我想我在Apple文档中的某个地方读过这篇文章:他们建议仅在需要填充可滚动可见区域时才创建如此的象征。

But I fear performance issues if I would instantly reposition UIImageView elements. 但是如果我立即重新定位UIImageView元素,我会担心性能问题。 I think. 我认为。 As the user starts to scrolls down (lets assume it starts at 0,0), I'd have to reposition those UIView objects that go out of visible area at the top, positioning them quickly to the bottom and then assigning very (!!) quickly an new image object to that UIImageViews in the row. 随着用户开始向下滚动(假设它从0,0开始),我必须重新定位那些不在顶部可见区域中的UIView对象,将它们快速定位到底部,然后分配非常大! )快速在该行中将新图像对象添加到该UIImageViews。 I mean... what if the user starts scrolling like a wild gazelle, faster and faster? 我的意思是...如果用户开始像野瞪羚一样滚动得越来越快怎么办?

How's your approach for doing that? 您的做法如何?

UIScrollView (or actually UIKit) is very good at not redrawing things that don't change. UIScrollView(或实际上是UIKit)非常擅长不重绘不变的内容。 If you only move (or remove to recycle) off screen (or clipped) views your will not incur a redraw. 如果仅在屏幕外(或修剪的)视图外移动(或删除以循环使用),则不会导致重画。 Ditto with adding subViews off screen. 同在屏幕外添加subViews的情况。

If you don't have paging enabled, you will probably have to implement some faster drawing code that UIView offers. 如果没有启用分页,则可能必须实现UIView提供的一些更快的绘图代码。 However I would test first - the physical limitations of the iPhone limit scrolling to some extent. 但是,我首先要测试-iPhone的物理限制在一定程度上限制了滚动。

In a similar, though smaller scale situation, I dealt with this by loading more than I needed for the scroll view dimensions, but not the entire data set. 在类似的规模较小的情况下,我通过加载比滚动视图尺寸所需的更多的内容来解决此问题,但没有加载整个数据集。 I was only scrolling horizontally and I found that loading 1 or 2 images on each side of the currently-visible one kept things nice and smooth. 我只是水平滚动,发现在当前可见的每一侧加载1或2张图像,可以使一切保持流畅。 That's still loading 3 or 5 images when only 1 is visible, but it's a hell of a lot better than loading up dozens of them. 当只能看到1幅图像时,仍然可以加载3幅或5幅图像,但这比加载数十幅图像要好得多。 You'd need to experiment a little to see how much slop you need to allow around the visible area. 您需要做一些试验,以查看在可见区域周围需要允许多少倾斜。

Dynamically loading/unloading images is best handled in the scroll view's delegate methods. 动态加载/卸载图像最好在滚动视图的委托方法中处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM