2012-03-15 211 views
2

如何添加漸變背景到UIScrollView?我有一個UIScrollView在其上的viewcontroller。我想爲scrollview添加漸變背景。我也有一個用於背景的漸變圖像,但它並不完全適合滾動視圖的內容大小。漸變背景UIScrollView

請幫忙。 謝謝

回答

8

我以前設置UIScrollViewbackgroundColor清除,然後在滾動視圖後面放置一個漸變視圖。然後畫上UIView像一個梯度:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 

    CGGradientRef glossGradient; 
    CGColorSpaceRef rgbColorspace; 
    size_t num_locations = 2; 
    CGFloat locations[2] = { 0.0, 1.0 }; 
    CGFloat components[8] = { 0.119, 0.164, 0.192, 1.000, // Start color 
     0.286, 0.300, 0.307, 1.000 }; // End color 

    rgbColorspace = CGColorSpaceCreateDeviceRGB(); 
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); 

    CGRect currentBounds = self.bounds; 
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f); 
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); 
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0); 

    CGGradientRelease(glossGradient); 
    CGColorSpaceRelease(rgbColorspace); 
} 

我不知道什麼樣的影響直接在UIScrollViewdrawRect將有繪製漸變。