Bad performance on scroll view loaded with 3 view controllers, drawn with CALayer

安稳与你 提交于 2019-12-12 17:25:53

问题


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

回答1:


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.

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 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.




回答2:


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/



来源:https://stackoverflow.com/questions/5195150/bad-performance-on-scroll-view-loaded-with-3-view-controllers-drawn-with-calaye

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!