问题描述
我创建了一个文档视图,它在角落显示页码.页码是一个带有半透明背景色的uilabel,并且有一个圆角半径(使用view
的layer
的cornerRadius
属性>).我已将其置于 UIScrollView
上方.但是,这会使滚动生涩.如果我删除 cornerRadius
,性能会很好.我能做些什么吗?什么是更好的解决方案?似乎在 UIWebView
中实现了,没有任何性能问题.
I've created a document view which displays the page number in the corner. The page number is a uilabel with a semi-transparent background colour, and has a corner radius (using the cornerRadius
property of the view
's layer
). I've positioned this above a UIScrollView
. However, this makes scrolling jerky. If I remove the cornerRadius
, performance is good. Is there anything I can do about this? What would be a better solution? It seems to have been achieved in the UIWebView
without any performance issues.
推荐答案
对于标签或带有圆角的视图和/或滚动视图上的背景颜色和阴影,解决方案非常简单:
For labels, or views with rounded corners and or background colors and shadows on scrolling views the solution is pretty simple:
最大的问题是来自 maskToBounds 层选项.这似乎会显着影响性能,但是标签似乎需要此 ON 以将背景颜色屏蔽为圆角.因此,要解决此问题,您需要改为设置标签图层背景颜色并关闭 maskToBounds.
The biggest issue is from the masksToBounds layer option. This appears to tax performance significantly, however the label seems to need this ON to mask the background color to rounded corners. So to get around this you need to set the labels layer background color instead and switch off masksToBounds.
第二个问题是默认行为是尽可能重绘视图,这对于滚动视图上的静态或缓慢变化的项目是完全没有必要的.这里我们简单地设置 layer.shouldRasterize = YES.这将允许 CA缓存"视图的光栅化版本,以便在滚动时快速绘制(大概是硬件加速).
The second issue is that the default behavior is to redraw the view whenever possible which is totally unnecessary with static or slow changing items on scrolling views. Here we simply set layer.shouldRasterize = YES. This will allow CA to 'cache' a rasterized version of the view for quick drawing when scrolling (presumably with hardware acceleration).
您需要确保您的图层具有 Alpha 通道,否则栅格化会影响圆角的绘制.我从来没有遇到过问题,因为我为我的背景颜色设置了 alpha,但您可能需要检查您的情况.
You need to make sure your layer has an alpha channel otherwise rasterizing will affect the drawing of rounded corners. I've never had a problem as I have alpha set for my background colors, but you may need to check in your situation.
这是一个设置为在 scollview 上很好地工作的示例 UILabel:
Here is a sample UILabel set up to work nicely on a scollview:
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 40.0, 24.0)];
lbl.font = [UIFont fontWithName:@"Helvetica" size:14.0];
lbl.textAlignment = UITextAlignmentRight;
lbl.text = @"Hello World";
// Must set the label background to clear so the layer background shows
lbl.backgroundColor = [UIColor clearColor];
// Set UILabel.layer.backgroundColor not UILabel.backgroundColor otherwise the background is not masked to the rounded border.
lbl.layer.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.5].CGColor;
lbl.layer.cornerRadius = 8;
lbl.layer.borderColor = [UIColor blackColor].CGColor;
lbl.layer.borderWidth = 1;
// Huge change in performance by explicitly setting the below (even though default is supposedly NO)
lbl.layer.masksToBounds = NO;
// Performance improvement here depends on the size of your view
lbl.layer.shouldRasterize = YES;
lbl.layer.rasterizationScale = [UIScreen mainScreen].scale;
// self here is the child view in the scroll view
[self addSubview:lbl];
[lbl release];
我可以用这样的视图填满 iPad 1 屏幕,并且仍然可以流畅地滚动:)
I can fill the iPad 1 screen with views like this and still scroll smoothly :)
这篇关于UILabel 层cornerRadius 对性能产生负面影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!