加载 3 个视图控制器(使用 CALayer 绘制)的滚动视图性能不佳
我有一个加载了 3 个视图控制器的滚动视图。每个视图控制器都使用该代码绘制其图层 - (还有更多,但我把它拿出来检查是否有帮助)。我的滑动仍然很糟糕。 有什么帮助吗?
沙尼
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [Helper cardBackGroundColor:card].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectInset(self.view.layer.frame, 20, 20);
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 2.0;
sublayer.cornerRadius = 10.0;
[self.view.layer addSublayer:sublayer];
i have a scroll view loaded with 3 view controllers. each view controller is drawing its layers with that code -
(there us more then that but I pulled it out to check if it will help). still i have very crappy sliding.
any help ?
shani
CALayer *sublayer = [CALayer layer];
sublayer.backgroundColor = [Helper cardBackGroundColor:card].CGColor;
sublayer.shadowOffset = CGSizeMake(0, 3);
sublayer.shadowRadius = 5.0;
sublayer.shadowColor = [UIColor blackColor].CGColor;
sublayer.shadowOpacity = 0.8;
sublayer.frame = CGRectInset(self.view.layer.frame, 20, 20);
sublayer.borderColor = [UIColor blackColor].CGColor;
sublayer.borderWidth = 2.0;
sublayer.cornerRadius = 10.0;
[self.view.layer addSublayer:sublayer];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 CALayer 绘制内容通常会产生较差的性能。我们通常使用可拉伸图像来获得足够的性能。当你想到这一点时,提前渲染它比使用 iPhone 有限的处理能力来实时渲染它确实有意义。
您可能可以从 CALayer 获得足够的性能,但绘制 png 可能仍然会更快,从而节省电池寿命。
编辑:所以这是一个解释这个概念的例子。
这段代码实际上取代了一个太慢的 CALayer 绘图。
shadow.png
大小为 34 x 34 像素,包含一个阴影方块。由于可拉伸图像,可以在不拉伸阴影的情况下调整正方形的大小。有关这方面的更多信息,我建议阅读stretchableImageWithLeftCapWidth:topCapHeight:
的文档。此外,Google 还将帮助您找到有关如何使用可拉伸图像的指南。如果您有更多问题,我很乐意回答。Drawing things with
CALayer
often yields poor performance. We usually use a stretchable image to get adequate performance. When you think of it, it does make sense to render it before hand rather than using the iPhone's limited processing power to render it in real time.It's possible that you can get adequate performance from
CALayer
, but drawing a png will probably still be faster, thus saving battery life time.EDIT: So here's an example to explain the concept.
This code actually replaced a CALayer drawing that was too slow.
shadow.png
is 34 by 34 pixels and contains a shadowed square. Thanks to the stretchable image it's possible to resize the square without stretching the shadow. For more information about this I would suggest reading the documentation forstretchableImageWithLeftCapWidth:topCapHeight:
. Also Google will help you find guides on how to work with stretchable images. If you have more questions I'll be happy to answer them.你有一个遮罩(假设你在某个地方说 maskToBounds=YES)和该层上的阴影。两者都会导致离屏渲染通道。
请观看 WWDC 2010 会议 425 - 核心动画实践第 2 部分,
您可以在此处找到;
http://developer.apple.com/videos/wwdc/2010/
You have a mask (assuming you somewhere say masksToBounds=YES) and a shadow on this layer. Both cause an off screen rendering pass.
Please watch the WWDC 2010 Session 425 - Core Animation in Practice Part 2
Which you can find here;
http://developer.apple.com/videos/wwdc/2010/