如何在大 uiscrollview (iphone) 上平滑地滚动 600ish uilabels
我有一个甘特图,我想在 iPhone 上显示它。
它的大小为 7200 x 1800px,由约 600 个条组成,每个条都是一个 UILabel。
它看起来像这样:
现在我已经让它工作了。在大约 100 个条时,我只需将它们全部添加到滚动视图中就可以使其运行得非常流畅。然而,当我实例化所有这些 uilabel 并将它们全部添加到滚动视图作为子视图时,如果有完整的 600 个(或最终更多),它就会崩溃。
所以我所做的就是让它只为当前可见的行创建 uilabels,并且当用户上下滚动时,它会删除不可见的 uilabels 并添加新的可见的 uilabels。
然而,当您垂直滚动穿过每行边界时,这种抖动会非常明显,并且必须渲染另一行并删除旧行。
有人有任何建议来解决这个问题吗?有什么想法是缓慢的部分吗?实例化 uilabels,或者将它们添加为子视图,或者其他什么?
我们将不胜感激所有帮助。
I have a bit gantt chart that i want to be visible on an iphone.
It is 7200 x 1800px large, and consists of ~600 bars, each of which is a UILabel.
It is to look like this:
Now i've gotten it to work. And at ~100 bars, i can make it run quite smoothly by simply adding them all to the scroll view. However, with the full 600 (or more eventually) it simply crashes when i instantiate all those uilabels and add them all to the scroll view as subviews.
So what i've done is made it create only the uilabels for the currently visible rows, and as the user scrolls up and down it removes the invisible uilabels and adds the newly visible ones.
However, this jerks quite noticeably as you scroll vertically as it crosses each row boundary, and has to render another row and remove the old row.
Does anyone have any suggestions to solve this? Any ideas what is the slow part? Instantiating the uilabels, or adding them as subviews, or anything?
All help will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
苹果有一些非常好的演示代码来展示如何做到这一点。查看 TiledScrollView.m 尤其是
layoutSubviews
方法。您可能需要考虑的其他事项:
如果您的标签水平相当长,您可能需要将它们分成更小的块。在这种情况下,相当长比屏幕宽。
确保您的 UILabel 是不透明的。滚动需要合成的内容会增加额外的开销,这可能会导致您出现一些问题。
查看您的屏幕截图,行标题和列标题不是不透明的,并且正在使用 Alpha。虽然这是一个很好的效果,但暂时将它们设置为不透明可能值得,只是为了看看这是否会导致您的问题。我不认为这对你的问题造成太大影响;正在合成的区域非常小。
Apple has some really good demo code that shows how to do this. Check out TiledScrollView.m especially the
layoutSubviews
method.Other things you might consider:
If you labels are quite long horizontally you may need to break them into smaller chunks. Quite long in this context is wider than the screen.
Make sure your UILabels are opaque. Scrolling things that require compositing adds extra overhead which may account for some of your issues.
Looking at your screen shot the row and column headers are not opaque and are using alphas. Whereas this is a nice effect it may be worth temporarily making them opaque too just to see if this is contributing to your problems. I don't think this is contributing too much to your problems; the area being composited is quite small.
只是一个想法,但问题可能是,即使您正在缓存和重用标签,滚动视图是否仍然保留它们,因此即使您可能只有几个标签,每个标签都会被保留数百次。如果是这样,那么我认为滚动视图仍然有效地尝试管理这数百行。
正如@Nathon S 所问的——你要移动它们吗?即构建一组有限的标签,然后在滚动视图上移动它们以匹配查看区域。如果您隐藏并重新添加到滚动视图,那么我会怀疑大量保留会减慢速度。我认为,通过移动标签设计,您不需要在初始显示后进行任何隐藏和添加。这应该使它非常快速且轻量。
Just a thought, but could the issue be that even though you are caching and reusing the labels, is the scroll view still retaining them, so even though you may only have a few labels, each is being retained hundreds of times. If this is so then I would think that the scroll view is still effectively trying to manage those hundreds of rows.
So as @Nathon S asked - are you moving them? i.e. building a finite set of labels and just moving them around on the scroll view to match the viewing area. If you are hiding and re-adding to the scroll view then I would suspect a massive set of retains being the slow down. I would think that with a moving label design, you would not need to do any hides and adds after the the initial display. Which should make it very fast and lightweight.