您的位置:首页 > 移动开发 > IOS开发

【深入浅出IOS开发】IOS绘图基础

2014-12-20 08:01 239 查看
1.绘图的常用步骤

①获得设备上下文

CGContextRef ctr =
UIGraphicsGetCurrentContext();
②在设备上下文绘图

CGContextMoveToPoint(ctr,
10, 10);
CGContextAddLineToPoint(ctr,
50, 50);
③渲染到相应的View

CGContextFillPath(ctr);

一般来说绘制图形,通常是CGContextAdd*****,而更改某些属性则是CGContextSet*****.
绘制圆弧的时候,通常使用:

CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);
其中参数的设置是以x轴为0,下面为+,上面为-。
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//draw3Tri();
//draw4Rect();
drawEcplise();
}

void drawEcplise()
{
CGContextRef ctr = UIGraphicsGetCurrentContext();

//  CGContextAddEllipseInRect(ctr, CGRectMake(20, 20, 100,200));
// CGContextAddArcToPoint(ctr, 10, 10, 50, 100, 100);
//CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);

CGContextMoveToPoint(ctr, 50, 50);
CGContextAddLineToPoint(ctr, 100, 100);

CGContextSetRGBStrokeColor(ctr, 1, 0, 0, 1);
CGContextSetLineWidth(ctr, 5);
CGContextStrokePath(ctr);

CGContextMoveToPoint(ctr, 150, 150);
CGContextAddLineToPoint(ctr, 200, 200);

CGContextSetRGBStrokeColor(ctr, 0, 0, 1, 1);
CGContextSetLineWidth(ctr, 2);
CGContextStrokePath(ctr);

}

void draw4Rect()
{
CGContextRef ctr = UIGraphicsGetCurrentContext();

CGContextAddRect(ctr, CGRectMake(10, 10, 80, 80));

CGContextFillPath(ctr);
}

void draw3Tri()
{
//1.获得设备上下文
CGContextRef ctr = UIGraphicsGetCurrentContext();

//2.在设备中绘图
CGContextMoveToPoint(ctr, 10, 10);
CGContextAddLineToPoint(ctr, 50, 50);
CGContextAddLineToPoint(ctr, 100, 0);
CGContextAddLineToPoint(ctr, 10, 10);
//3.渲染到view中
//CGContextStrokePath(ctr);
CGContextFillPath(ctr);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: