使用CALayer绘制的加载有3个视图控制器的滚动视图上的性能不佳 [英] Bad performance on scroll view loaded with 3 view controllers, drawn with CALayer

查看:48
本文介绍了使用CALayer绘制的加载有3个视图控制器的滚动视图上的性能不佳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个滚动视图,其中装有3个视图控制器。每个视图控制器都使用该代码-
绘制其图层(还有更多代码,但我将其拉出以检查是否有帮助)。还是我很烂。
任何帮助吗?

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];


推荐答案

使用 CALayer 通常会产生较差的性能。我们通常使用可拉伸的图像以获得足够的性能。当您想到它时,事前渲染确实有意义,而不是使用iPhone的有限处理能力来实时渲染它。

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.

有可能从 CALayer 获得足够的性能,但是绘制png可能仍然会更快,从而节省了电池寿命。

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.

编辑:因此,这里有一个示例来解释这个概念。

So here's an example to explain the concept.

此代码实际上替代了速度太慢的CALayer绘图。

This code actually replaced a CALayer drawing that was too slow.

UIImageView *shadow = [[UIImageView alloc] initWithFrame:frame];
shadow.image = [[UIImage imageNamed:@"shadow.png"] stretchableImageWithLeftCapWidth:16.0 topCapHeight:16.0];
[contentView addSubview:shadow];
[shadow release];

shadow.png 是34 x 34像素并包含阴影正方形。借助可拉伸的图像,可以在不拉伸阴影的情况下调整正方形的大小。有关此的更多信息,我建议阅读 stretchableImageWithLeftCapWidth:topCapHeight:的文档。 Google还将帮助您找到有关如何处理可拉伸图像的指南。如果您还有其他问题,我们很乐意为您解答。

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 for stretchableImageWithLeftCapWidth: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.

这篇关于使用CALayer绘制的加载有3个视图控制器的滚动视图上的性能不佳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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