【发布时间】:2012-03-31 13:52:58
【问题描述】:
如何给 UIScrollView 添加渐变背景?我有一个带有 UIScrollView 的视图控制器。我想将渐变背景添加到滚动视图。我也有一个渐变图像作为背景,但它并不完全适合滚动视图的内容大小。
请帮忙。 谢谢
【问题讨论】:
标签: iphone objective-c ios uiviewcontroller uiscrollview
如何给 UIScrollView 添加渐变背景?我有一个带有 UIScrollView 的视图控制器。我想将渐变背景添加到滚动视图。我也有一个渐变图像作为背景,但它并不完全适合滚动视图的内容大小。
请帮忙。 谢谢
【问题讨论】:
标签: iphone objective-c ios uiviewcontroller uiscrollview
我之前将UIScrollView backgroundColor 设置为clear,然后将渐变视图定位在滚动视图后面。然后在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);
}
不知道直接在UIScrollView的drawRect中绘制渐变会有什么效果。
【讨论】:
从https://github.com/techiedees/GradientTest链接下载示例项目。
它有 TDGradientView 框架。只需将它用于您想要的任何项目。
您可以在http://www.techiedees.com/2012/04/how-to-create-gradient-view-at-run-time.html查看它
【讨论】: