2010-02-13 28 views
8

我在表格視圖中爲每個單元格添加了一個UILabel。這最初沒有問題。當我使用layer.cornerRadius滾動UILabel的角落時,滾動表視圖研磨停止。當向每個單元格添加帶cornerRadius的UILabel時,UITableView進入爬行狀態

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]; 
+0

你可能想看看[Tweetie如何做](http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/)(順利滾動)。 – icktoofay 2010-02-13 05:55:11

回答

7

我在我自己之前試過這個,發現它對任何移動的視圖都沒用。您應該創建一個帶圓角的圖像,並將其添加爲標籤的背景。

+0

這不是一個非常好的解決方案,因爲在不同尺寸的標籤上,圖像會變得拉伸,圖像給出的角落半徑會與其他圖像不同。 – 2014-07-30 08:54:03

15

沒有必要與rouned角落的圖像 - 看到fknrdcls回答這個問題:

UILabel layer cornerRadius negatively impacting performance

基本上,你只需給你的標籤,透明背景,並添加的backgroundColor改爲圖層。然後,您可以禁用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; 
+0

+1我真的很喜歡這個解決方案,但我只需要**一些角落圓潤的**。我的解決方法是:爲了只顯示右上角和右下角,我在其frame.origin.x中添加了一個負值的子視圖,並將cornerRadius分配給它的圖層。如果有人找到了更好的解決方案,我很感興趣。 – anneblue 2014-03-04 12:02:59

0

一個的UIImageView,因爲我不得不裁剪圖像爲好,不只是一個背景色的UILabel招沒有奏效。我發現的最快捷的解決方案是在動畫的時候對視圖的父節點進行光柵化處理(我有一個滑動面板)。

相關問題