CoreGraphics详解
- CoreGraphics详解
- CoreGraphics绘图
- 绘制一个矩形
- 绘制一个椭圆
- 绘制曲线
- 绘制圆形
- 链接点来绘制为图形
- UIView四周描线
- CoreGraphics绘图
CoreGraphics绘图
绘制一个矩形
CGRect rectangle=CGRectMake(0, 0, 200, 200);
//获取当前图形
CGContextRef ctx=UIGraphicsGetCurrentContext();
//当前图形添加矩形
CGContextAddRect(ctx, rectangle);
//填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
//绘制当前区域
CGContextFillPath(ctx);
绘制一个椭圆
CGRect rectangle=CGRectMake(30, 30, 200, 200);
//获取当前图形
CGContextRef ctx=UIGraphicsGetCurrentContext();
//当前图形添加椭圆
CGContextAddEllipseInRect(ctx,rectangle);
//填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
//绘制当前区域
CGContextFillPath(ctx);
###绘制一个椭圆
CGRect rectangle=CGRectMake(30, 30, 200, 200);
//获取当前图形
CGContextRef ctx=UIGraphicsGetCurrentContext();
//当前图形添加椭圆
CGContextAddEllipseInRect(ctx,rectangle);
//填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
//绘制当前区域
CGContextFillPath(ctx);
绘制曲线
//get the current drawing context
CGContextRef ctx=UIGraphicsGetCurrentContext();
//start drawing with point
CGContextBeginPath(ctx);
//draw to this point
CGContextMoveToPoint(ctx, 200.0, 200.0);
//draw 贝塞尔曲线
CGContextAddQuadCurveToPoint(ctx, 150, 220, 150, 300);
//fill color
/**
* @brief 在指定点追加二次贝塞尔曲线,通过控制点和结束点指定曲线。
* 关于曲线的点的控制见下图说明,图片来源苹果官方网站。参数按顺序说明
* @param c 当前图形
* @param cpx 曲线控制点的x坐标
* @param cpy 曲线控制点的y坐标
* @param x 指定点的x坐标值
* @param y 指定点的y坐标值
*
*/
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextSetLineWidth(ctx, 10);
//draw this area
CGContextStrokePath(ctx);
绘制圆形
//获取当前图形
CGContextRef ctx=UIGraphicsGetCurrentContext();
//当前图形添加椭圆
CGContextAddArc(ctx, 50.0, 50.0, 25.0, 0, 2 * M_PI, 1);
//填充颜色
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
//绘制当前区域
CGContextFillPath(ctx);
链接点来绘制为图形
CGContextRef ctx=UIGraphicsGetCurrentContext();
//start drawing with point
CGContextBeginPath(ctx);
//draw to this point
CGContextMoveToPoint(ctx, 100.0, 100.0);
CGContextAddLineToPoint(ctx, 100.0, 200.0);
CGContextAddLineToPoint(ctx, 200.0, 100.0);
//return to orgin point
CGContextClosePath(ctx);
//fill color
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
//绘制当前区域
CGContextFillPath(ctx);
UIView四周描线
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
//get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSetLineWidth(context,1);
CGContextMoveToPoint(context, 0, 0);
CGContextAddLineToPoint(context,
CGRectGetWidth(self.bounds),
0);
CGContextMoveToPoint(context,
CGRectGetWidth(self.bounds),
0);
CGContextAddLineToPoint(context,
CGRectGetWidth(self.bounds),
CGRectGetHeight(self.bounds));
CGContextMoveToPoint(context,
CGRectGetWidth(self.bounds),
CGRectGetHeight(self.bounds));
CGContextAddLineToPoint(context,0,CGRectGetHeight(self.bounds));
CGContextMoveToPoint(context,0,
CGRectGetHeight(self.bounds));
CGContextAddLineToPoint(context,0,0);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//线条颜色
CGContextStrokePath(context);
}