渐变背景UIScrollView
问题描述:
如何添加渐变背景到UIScrollView?我有一个UIScrollView在其上的viewcontroller。我想为scrollview添加渐变背景。我也有一个用于背景的渐变图像,但它并不完全适合滚动视图的内容大小。渐变背景UIScrollView
请帮忙。 谢谢
答
我以前设置UIScrollView
backgroundColor
清除,然后在滚动视图后面放置一个渐变视图。然后画上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