我创建了一个文档视图,在角落显示页码。页码是一个具有半透明背景色的uilabel,并且有一个角半径(使用cornerRadius
的财产view
's layer
)。我已将其放置在UIScrollView
。然而,这会使滚动变得不稳定。如果我删除cornerRadius
,性能良好。我能做些什么吗?什么是更好的解决方案?看来已经实现了UIWebView
没有任何性能问题。
对于标签或带有圆角的视图和/或滚动视图上的背景颜色和阴影,解决方案非常简单:
最大的问题来自 maskToBounds 图层选项。这似乎对性能产生了很大的影响,但是标签似乎需要打开此选项来将背景颜色掩盖到圆角。因此,要解决此问题,您需要设置标签图层背景颜色并关闭 maskToBounds。
第二个问题是,默认行为是尽可能重绘视图,这对于滚动视图上的静态或缓慢变化的项目来说是完全没有必要的。这里我们简单设置layer.shouldRasterize = YES。这将允许 CA“缓存”视图的光栅化版本,以便在滚动时快速绘制(可能使用硬件加速)。
您需要确保您的图层具有 Alpha 通道,否则光栅化会影响圆角的绘制。我从来没有遇到过问题,因为我为背景颜色设置了 Alpha,但您可能需要检查您的情况。
这是一个设置为在滚动视图上很好地工作的 UILabel 示例:
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 屏幕,并且仍然可以平滑滚动:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)