有效地子类化 UIView
伙计们,我在子类化 UIView 时遇到了一些麻烦。 我正在创建一个 IconView。 简而言之,它是其他一些子视图的容器。 在我的 IconView 中我有这个 iVar: UIImageView_背景 UIImageView_icon UILabel_iconLabel。
当我初始化 IconView 时,我使用图像、文本和一些石英效果(如 roundCorner 和 Shadow)设置了这 3 个 iVar,然后将它们添加到自身视图中。 一切都很好,但是如果我在空滚动视图中插入一些 IconView (即 10),则滚动效果不平滑。我在滚动视图中插入数千个简单的 UIView 之前尝试过,并且滚动动画效果完美。 我的 IconView 只有 10 个,滚动动画效果非常糟糕。
我可以采取不同的方法保留 UIImages 而不是 UIImageViews 并将其绘制在 drawRect: 方法中,但在这种情况下,我将失去 Autoresizing 属性和 Quartz 效果。
有什么建议吗? 谢谢,加布里埃尔。
Guys I'm having some troubles subclassing an UIView.
I'm creating an IconView.
Simply it's a container for some other subviews.
In my IconView i have this iVar:
UIImageView _background
UIImageView _icon
UILabel _iconLabel.
When I initialize the IconView I setup this 3 iVar with images, text and some quartz effect like roundCorner and Shadow and then I add them to the self view.
Everything is Ok but if I insert some of this IconView (i.e. 10) inside an empty scrollview the scroll effect is not smooth. I tried before inserting thousand of simple UIViews in a scrollview and the scroll animation works perfectly.
With just 10 of my IconView the scroll animation works really bad.
I could approach differently retaining UIImages instead of UIImageViews and draw it inside drawRect: method but in this case I'm gonna loose Autoresizing property and Quartz effect.
Any suggests?
Thank, Gabriele.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,
UIScrollView
变得非常快。有很多关于这个主题的帖子和文章,比如 这个问题< /a> 和 这篇(缺陷)博文以及它的示例代码。 2010 年 WWDC 视频中还有三个关于“iOS 中的性能优化”的会议,我强烈推荐观看。总结结论:使用尽可能少的子视图,并特别注意避免透明度。好的,关于“ScrollViews 中的性能”的一般性讨论就到此为止,现在谈谈您的案例:遇到同样的问题,我使用了上面文章和视频中的所有技巧,虽然它们提高了性能,但还不够。我和你一样,在一些图像中使用了圆角,我发现这绝对会降低性能。仅仅停用它们比其他任何事情都更有帮助。阴影效果可能是一样的。
现在,您很可能希望保留这些圆角。我建议您创建图像的副本(或获取原始图像,如果可能),然后使用 那些很棒的类。这样,效果只会应用一次。它非常适合我。对于阴影,您可以在 Photoshop 中创建一些阴影并在新的 ImageView 中使用它们。
如果这还不够,您应该尝试缓存
IconViews
,就像缓存TableViewCells
一样(如果您还没有这样做的话)。Unfortunately, a
UIScrollView
gets slow pretty fast. There are a lots of posts and articles on this topic out there, like this Question and this (defect) blogpost along with it's sample code. There are also three sessions about 'Performance optimization in iOS' in the 2010's WWDC videos which I highly recommend to watch. To summarize the conclusions: Use as few subviews as you can and take special care of avoiding transparencies.Ok, so much for the general 'Performance in ScrollViews' talk, now to your case: Having the same problem, I used all the tips from the articles and videos above and while they improved the performance, it just wasn't enough. I had, like you, used rounded corners one some images and I found out that this absolutely kills performance. Just deactivating them helped more than everything else. It's probably the same with the shadow effects.
Now, most likely, you want to keep those rounded corners. I would suggest that you create a copy of your images (or take the original, if possible) and than manipulate them directly, using those awesome classes. This way, the effects will only be applied once. It works perfectly for me. For you shadows, you can probably just create some in Photoshop and use them in a new ImageView.
If that isn't enough, you should try to cache your
IconViews
, likeTableViewCells
are cached, if you don't already do this.问题可能是石英阴影。如果大量使用它们确实会减慢渲染速度。
在注销它们之前,您可以尝试将 CALayer 的 shouldRasterize 属性设置为 YES。这使得石英仅渲染阴影一次并将其存储在缓冲区中。看看进展如何。
The problem will probably be the Quartz shadows. They can really slow the rendering down if used a lot.
Before you write them off, you might try setting your CALayer's shouldRasterize property to YES. This makes quartz render the shadow only once and store it in a buffer. See how it goes.