2011-06-28 58 views
4

我自定義我的UITableView,我想出瞭如何設置每個單元格的選定顏色。在我cellForRowAtIndexPath的方法,我有以下代碼:漸變imageView iOS

UIView *bgColorView = [[UIView alloc] init]; 
    [bgColorView setBackgroundColor:[UIColor orangeColor]]; 
    [cell setSelectedBackgroundView:bgColorView]; 
    [bgColorView release]; 

但它是一個堅實的橙色。我想讓它看起來更光滑,並且從淺橙色到深橙色都有輕微的漸變。我怎樣才能做到這一點?

回答

3

你會使用核芯顯卡(又名石英)在您的視圖的-drawRect:方法來繪製一個漸變:

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGFloat colors[8] = {1.0, 0.75, 0.30, 0.5, 0.7, 0.2, 1.0, 0.8}; 
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); 
CGGradientRef gradient = CGGradientCreateWithColorComponents(space, colors, NULL, 2); 
CGContextDrawLinearGradient(ctx, gradient, top, bottom, NULL); 

呦您可以通過在當前上下文(ctx)中創建路徑以及使用CGContextClip(ctx);對其進行裁剪來限制漸變填充的區域。 topbottom是CGPoints,它們定義了漸變的開始和結束。

1

@ Caleb的答案是正確的;我爲此做了各種各樣的事情。

沒有人提到的是,您需要實施的視圖drawRect:是自定義UITableViewCell

0

如果你想要一個複雜或微妙的漸變,你可以設置一個背景圖像是部分alpha,並設置單元格背景顏色來改變外觀。