UITableView comes to a crawl when adding a UILabel with cornerRadius to each cell

孤人 提交于 2019-11-29 02:13:16

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.

ChristophK

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:

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;

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!