Iphone iOS的自定义渐变背景视图不工作
我有以下UIView自定义类:Iphone iOS的自定义渐变背景视图不工作,iphone,ios,uiview,gradient,Iphone,Ios,Uiview,Gradient,我有以下UIView自定义类: @implementation BackgroundView - (void)drawRect:(CGRect)rect { CGContextRef currentContext = UIGraphicsGetCurrentContext(); CGGradientRef glossGradient; CGColorSpaceRef rgbColorspace; size_t num_locations = 2;
@implementation BackgroundView
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
float r = (255.0)/(255.0);
float g = (165.0)/(255.0);
float b = (0.0)/(255.0);
float rr = (238.0)/(255.0);
float gg = (118.0)/(255.0);
float bb = (0.0)/(255.0);
CGFloat components[8] = { r, g, b, 1.0, // Start color
rr, gg, bb, 1.0 }; // 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), CGRectGetMidY(currentBounds));
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
}
@end
基本上,它应该是一个橙色的梯度。值r、g和b表示浅橙色,值rr、gg、bb表示深橙色。但它不起作用。只有框架顶部为橙色,框架底部为黑色。我似乎无法摆脱黑色
我会用什么值来表示橙色的渐变呢。有人能找出错误吗?我找不到。非常感谢 这是因为您指定渐变的终点位于currentBounds的中间-请参见此行:
CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
要去除黑色,请确保y坐标位于视图的底部
或者,您可以使用此代码在端点之后扩展渐变
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, kCGGradientDrawsAfterEndLocation);