【问题标题】:UITableView comes to a crawl when adding a UILabel with cornerRadius to each cell将带有cornerRadius的UILabel添加到每个单元格时,UITableView会爬行
【发布时间】:2010-02-13 02:59:36
【问题描述】:

我正在为表格视图中的每个单元格添加一个 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];

【问题讨论】:

标签: ios uitableview uilabel cornerradius


【解决方案1】:

不需要带有圆角的图像 - 请参阅 fknrdcls 对此问题的回答:

UILabel layer cornerRadius negatively impacting performance

基本上,您只需为标签提供透明背景,然后将背景颜色添加到图层即可。然后,您可以禁用maskToBounds,这大大提高了性能。所以你的代码变成了:

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = [UIColor clearColor];
label1.layer.backgroundColor=[UIColor whiteColor].CGColor;
label1.layer.cornerRadius = 10.0f;
label1.layer.masksToBounds = NO;
label1.layer.shouldRasterize = YES;

【讨论】:

  • +1 我真的很喜欢这个解决方案,但我需要只需要一些圆角。我的解决方法是:要“显示”仅右上角和右下角,我在其frame.origin.x 上添加一个带有负值的子视图,并将cornerRadius 分配给它的layer。如果有人为该需求找到了更好的解决方案,我很感兴趣。
【解决方案2】:

我自己之前尝试过这个,发现它对于任何移动的视图都没有用。您应该创建一个带有圆角的图像并将其添加为标签的背景。

【讨论】:

  • 这不是一个很好的解决方案,因为在不同尺寸的标签上,图像会被拉伸,并且图像给出的角半径会与其他的不同..
【解决方案3】:

对于 UIImageView,UILabel 技巧不起作用,因为我还必须裁剪图像,而不仅仅是背景颜色。我发现最快的解决方案是在动画时栅格化视图的父级(我有一个滑动面板)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-17
    • 2015-04-10
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多