绘制过程:
创建上下文环境 -CGContextRef UIGraphicsGetCurrentContext (void); //CGContextRef currentContext = UIGraphicsGetCurrentContext();
保存状态 -void CGContextSaveGState (CGContextRef c); // CGContextSaveGState(currentContext);
创建颜色渐变对象:
创建色彩空间范围 -CGColorSpaceRef CGColorSpaceCreateDeviceRGB(void); // CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
标识颜色构成的数组,包括渐变中的起始颜色,结束颜色,每种颜色包括四个部分:red,green,blue,alpha -CGFloat colorComponents[8]
标识颜色位置的数组 - CGFloat colorIndices[2]
创建颜色渐变-CGGradientRef CGGradientCreateWithColorComponents(
CGColorSpaceRef space,
const CGFloat components[],
const CGFloat locations[],
size_t count
);
释放色彩空间-void CGColorSpaceRelease(CGColorSpaceRef cs); // CGColorSpaceRelease(colorSpace);
绘制渐变:
- void CGContextDrawLinearGradient(
CGContextRef context,
CGGradientRef gradient,
CGPoint startPoint, //渐变的起点
CGPoint endPoint, //渐变的终点
CGGradientDrawingOptions options
//绘制选项:KCGGradientDrawsAfterEndLocation | KCGGradientDrawsBeforeStartLocation | 0
//当起点,终点不在边界范围内时,边界颜色的延伸处理
);
释放色渐变-void CGGradientRelease(CGGradientRef gradient); // CGGradientRelease(gradient);
恢复初始状态 -void CGContextRestoreGState (CGContextRef c); //CGContextRestoreGState(currentContext);
- (void)drawRect:(CGRect)rect{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//颜色构成
UIColor *startColor = [UIColor blueColor];
CGFloat *startColorComponents =(CGFloat *)CGColorGetComponents([startColor CGColor]); // components: 包括red,green,blue,alpha
UIColor *endColor = [UIColor greenColor];
CGFloat *endColorComponents =(CGFloat *)CGColorGetComponents([endColor CGColor]);
CGFloat colorComponents[8] = {
/* Four components of the blue color (RGBA) */
startColorComponents[0],
startColorComponents[1],
startColorComponents[2],
startColorComponents[3], /* First color = blue */
/* Four components of the green color (RGBA) */
endColorComponents[0],
endColorComponents[1],
endColorComponents[2],
endColorComponents[3], /* Second color = green */
};
CGFloat colorIndices[2] = { //标识颜色
0.0f, /* Color 0 in the colorComponents array */
1.0f, /* Color 1 in the colorComponents array */
};
CGGradientRef gradient =CGGradientCreateWithColorComponents
(colorSpace,
(const CGFloat *)&colorComponents,
(const CGFloat *)&colorIndices,
2);
CGColorSpaceRelease(colorSpace);
//开始绘制
CGPoint startPoint, endPoint;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
/* 渐变的起点,终点决定了渐变的方向,如横向渐变,斜向渐变 */
/* 渐变一
startPoint = CGPointMake(0.0f,screenBounds.size.height / 2.0f);
endPoint = CGPointMake(screenBounds.size.width,startPoint.y);
CGContextDrawLinearGradient (currentContext,gradient,startPoint,endPoint,0);
*/
/* 渐变二
startPoint = CGPointMake(120,260);
endPoint = CGPointMake(200.0f,220);
CGContextDrawLinearGradient (currentContext,gradient,startPoint,endPoint,
kCGGradientDrawsBeforeStartLocation |
kCGGradientDrawsAfterEndLocation);
*/
// 渐变三
startPoint = CGPointMake(120,260);
endPoint = CGPointMake(200.0f,220);
CGContextDrawLinearGradient (currentContext,gradient,startPoint,endPoint,0);
CGGradientRelease(gradient);
CGContextRestoreGState(currentContext);
}