当向每个单元格添加带有cornerRadius的UILabel时,UITableView变得缓慢
我正在向表格视图中的每个单元格添加一个 UILabel。这最初没有问题。当我使用layer.cornerRadius
圆化 UILabel 的角时,滚动表视图会停止。
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = ([[managedObject valueForKey:@"color"] hasPrefix:@"FFFFFF"]) ? [UIColor blackColor] : color;
label1.layer.cornerRadius = 10.0f;
[cell addSubview:label1];
I'm adding a UILabel to each cell in my table view. This presents no problem initially. When I round the corners of the UILabel using layer.cornerRadius
scrolling the table view grinds to a halt.
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = ([[managedObject valueForKey:@"color"] hasPrefix:@"FFFFFF"]) ? [UIColor blackColor] : color;
label1.layer.cornerRadius = 10.0f;
[cell addSubview:label1];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不需要带有圆角的图像 - 请参阅 fknrdcls 对这个问题的回答:
UILabel 图层cornerRadius 对性能产生负面影响
基本上,您只需为标签提供透明背景,然后将背景颜色添加到图层即可。然后,您可以禁用
maskToBounds
,这会大大提高性能。所以你的代码变成:There is no need for an image with rouned corners - see the answer by fknrdcls to this question:
UILabel layer cornerRadius negatively impacting performance
Basically, you simply need to give your label a transparent background, and add the backgroundcolor to the layer instead. Then, you can disable
maskToBounds
, which greatly improves performance. So your code becomes:我自己之前就尝试过这个方法,发现它对于任何类型的移动视图都是没有用的。您应该创建一个带有圆角的图像,并将其添加为标签的背景。
I've tried this before myself and found that it's useless for any kind of view that moves. You should create an image with rounded corners and add that as the background of your label instead.
对于 UIImageView,UILabel 技巧不起作用,因为我还必须裁剪图像,而不仅仅是背景颜色。我发现的最快的解决方案是每当我对视图进行动画处理时就对其父级进行光栅化(我有一个滑动面板)。
for an UIImageView the UILabel trick didn't work because I had to crop an image as well, not just a background color. the quickest solution I found was to rasterize the view's parent whenever I animated it (I have a sliding panel).