0

I have a UIScrollView that is animating a lot of UIViews (probably too many). Also, these UIViews represent "pages", and sometimes multiple pages are stacked on top of each other, resulting in a "pile" of pages (setup by adding subviews to a given view).

I know that scrolling an excessive number of UIViews can have poor performance, but I was wondering if anybody had some general tips for me to improve performance?

For now, doing drawing manually in drawRect is not something I would like to consider because it would mess up various "page pile" animations. I will keep it in mind for a last resort, but I'd definitely like to avoid it if possible.

Update: The cause of the performance hit has been determined and is two fold: I'm using antialiasing and shadows on all my UIViews. When I toggle them both off, the performance issues are resolved! However, I obviously don't want to just toggle them off :)

I'm creating my shadows like so:

self.imageView.layer.opaque = YES;
        self.imageView.layer.masksToBounds = NO;
        self.imageView.layer.shadowOffset = CGSizeMake(-4, 0);
        self.imageView.layer.shadowRadius = 2.5;
        self.imageView.layer.shadowOpacity = 0.15;
        self.imageView.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(
                                                                                      self.bounds.origin.x,
                                                                                      self.bounds.origin.y,
                                                                                      self.bounds.size.width + 8,
                                                                                      self.bounds.size.height + 2)].CGPath;

Any tips to improve the performance?

As far as antialiasing, it is almost a necessity. The problem is that those "offset pages" are slightly rotated AND my pages have a 1 pixel border. Slightly rotated with a 1 pixel border without antialiasing looks awful. I am simply enabling antialiasing in the .plist by setting "Renders with Edge Antialiasing" to YES.

Any suggestions on how to improve my shadow/antialiasing performance would be appreciated.

4

1 回答 1

2

有多少 UIView 是“很多”?我们在谈论什么样的观点?如果不需要复杂的绘图,100 个 UIView 通常不是问题。但是,UIWebView呈现 PDF 的 10 个实例是另一回事……

确保您的视图仅在必要时进行布局和绘制(= 仅当它们实际可见时)。例如,您可以通过在视图中创建断点来检查这一点layoutSubviews

UIView此外,尽可能使用不透明元素。这使得绘图更加高效,因为不必绘制不透明元素下方的视图。

如果您碰巧有需要大量处理但很少更改的自定义CALayer实例(例如CAShapeLayer,带有阴影等),您可能需要考虑在这些实例上启用光栅化:yourLayer.shouldRasterize = YES; 这通过缓存渲染的合成图像来加速绘图。

于 2012-10-15T15:26:35.037 回答