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

ios- 画图

2015-09-07 07:54 459 查看
//5.画扇形

-(void)drawArc:(CGContextRef)contextRef
{
//圆心点---少句代码
// CGContextMoveToPoint(contextRef, 100, 100);
/**
*  画扇形
*
*  @param c#>          作用域 description#>
*  @param x#>          圆心x坐标 description#>
*  @param y#>          圆心y坐标 description#>
*  @param radius#>     半径 description#>
*  @param startAngle#> 开始角度 description#>
*  @param endAngle#>   结束角度 description#>
*  @param clockwise#>  方向(默认是顺时针0,1是逆时针) description#>
*
*  @return 扇形
*/
//    CGContextAddArc(contextRef, 100, 100, 50, 0, 45*M_PI/180, 10);
//    CGContextAddLineToPoint (contextRef, 100, 100);
//    CGContextFillPath(contextRef);

CGContextMoveToPoint(contextRef, 150, 150);
CGContextAddArc(contextRef, 150, 150, 100, 0, 270*M_PI/180, 1);
CGContextSetFillColorWithColor(contextRef, [UIColor purpleColor].CGColor);
CGContextFillPath(contextRef);

CGContextMoveToPoint(contextRef, 150, 150);
CGContextAddArc(contextRef, 150, 150, 100, 0,120*M_PI/180, 0);
CGContextSetFillColorWithColor(contextRef, [UIColor orangeColor].CGColor);
CGContextFillPath(contextRef);

CGContextMoveToPoint(contextRef, 150, 150);
CGContextAddArc(contextRef, 150, 150, 100, 120*M_PI/180, 270*M_PI/180, 0);
CGContextSetFillColorWithColor(contextRef, [UIColor blueColor].CGColor);
CGContextFillPath(contextRef);
}

//4.画圆
-(void)drawCircle:(CGContextRef)contextRef{
//在矩形里面画椭圆(即 矩形内切圆)
CGContextAddEllipseInRect(contextRef, CGRectMake(50, 50, 100, 100));

CGContextFillPath(contextRef);
}

//3.画矩形
-(void)drawRectWithContext:(CGContextRef)contextRef{
//矩形框
CGContextAddRect(contextRef, CGRectMake(100, 100, 100, 100));
//空心(画线的轨迹)
CGContextStrokePath(contextRef);
//实心(显示填充)
CGContextFillPath(contextRef);
//同时显示线框和填充
CGContextDrawPath(contextRef, kCGPathFillStroke);
//以上三种渲染方式只能使用一种,如果都写了,只执行先写的那一种
}

-(void)drawLine:(CGContextRef)contextRef{
//起点 路径的设置
CGContextMoveToPoint(contextRef, 20, 100);
//终点
CGContextAddLineToPoint(contextRef, 200, 200);
//设置宽度
CGContextSetLineWidth(contextRef, 25);
//设置颜色
CGContextSetRGBStrokeColor(contextRef, 1, 0, 1, 1);

CGContextSetStrokeColorWithColor(contextRef, [UIColor blueColor].CGColor);
//设置线的风格
CGContextSetLineCap(contextRef, kCGLineCapButt);

//画虚线
/**
*  <#Description#>
*
*  @param contextRef 作用域
*  @param phase#>    起点的左移量
*  @param lengths#>  规定实心和虚心的长度
*  @param count#>    实心和虚心的循环次数(count必须等于lengths数组的长度)
*
*  @return 虚线
*/

CGFloat lengths[] = {10,10};

CGContextSetLineDash(contextRef, 0, lengths, 2);

//画上来 将图形绘制到View上面(渲染)
CGContextStrokePath(contextRef);

}

-(void)drawTriangle:(CGContextRef)contextRef{
CGContextMoveToPoint(contextRef, 0, 0);

CGContextAddLineToPoint(contextRef, 100, 100);

//如果连续添加多条线 他会把上一条线的终点作为自己的起点(实现直线)
CGContextAddLineToPoint(contextRef, 150, 80);

CGContextSetLineWidth(contextRef, 10);
//折现角的风格样式
CGContextSetLineJoin(contextRef,kCGLineJoinRound);

//CGContextAddLineToPoint(contextRef, 0, 0);
//连接起点和终点封起来
CGContextClosePath(contextRef);

CGContextSetStrokeColorWithColor(contextRef, [UIColor redColor].CGColor);

//针对图形的填充方式,选择对应的状态进行设置
CGContextSetFillColorWithColor(contextRef, [UIColor yellowColor].CGColor);

//绘制空心的图形
//CGContextStrokePath(contextRef);
//绘制实心的图形
CGContextFillPath(contextRef);
}

-(void)drawBike:(CGContextRef)contextRef
{
//前轮
CGContextAddEllipseInRect(contextRef, CGRectMake(50, 300, 100, 100));
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//后轮
CGContextAddEllipseInRect(contextRef, CGRectMake(250, 300, 100, 100));
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//链条后轮
CGContextAddEllipseInRect(contextRef, CGRectMake(290, 340, 20, 20));
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//链条前轮
CGContextAddEllipseInRect(contextRef, CGRectMake(200, 330, 40, 40));
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//链条
//起点 路径的设置
CGContextMoveToPoint(contextRef, 220, 330);
//终点
CGContextAddLineToPoint(contextRef, 300, 340);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//起点 路径的设置
CGContextMoveToPoint(contextRef, 220, 370);
//终点
CGContextAddLineToPoint(contextRef, 300, 360);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//车架
//起点 路径的设置
CGContextMoveToPoint(contextRef, 100, 350);
//终点
CGContextAddLineToPoint(contextRef, 123, 230);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//起点 路径的设置
CGContextMoveToPoint(contextRef, 220, 330);
//终点
//CGContextAddLineToPoint(contextRef, 120, 250);
CGContextAddLineToPoint(contextRef, 240, 240);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//车梁
//起点 路径的设置
CGContextMoveToPoint(contextRef, 120, 250);
//终点
//CGContextAddLineToPoint(contextRef, 120, 250);
CGContextAddLineToPoint(contextRef, 252, 250);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//起点 路径的设置
CGContextMoveToPoint(contextRef, 250, 250);
//终点
//CGContextAddLineToPoint(contextRef, 120, 250);
CGContextAddLineToPoint(contextRef, 300, 350);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//车座
CGContextMoveToPoint(contextRef, 200, 230);

CGContextAddLineToPoint(contextRef, 250, 242);

//如果连续添加多条线 他会把上一条线的终点作为自己的起点(实现直线)
CGContextAddLineToPoint(contextRef, 250, 230);
CGContextSetLineWidth(contextRef, 5);
CGContextClosePath(contextRef);
//折现角的风格样式
CGContextSetLineJoin(contextRef,kCGLineJoinRound);
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//画曲线
//CGContextMoveToPoint(contextRef, 150, 150);
CGContextAddArc(contextRef, 100, 350, 70, -0.5, 280*M_PI/180, 1);
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

CGContextAddArc(contextRef, 250, 250, 111, 2.05, 2.5, 0);
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//车把
CGContextAddArc(contextRef, 160, 170, 70, 1.7, 2.5, 0);
CGContextSetLineWidth(contextRef, 5);
CGContextSetLineCap(contextRef, kCGLineCapRound);
CGContextStrokePath(contextRef);

//脚蹬下
CGContextMoveToPoint(contextRef, 220, 350);
//终点
CGContextAddLineToPoint(contextRef, 230, 380);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

CGContextMoveToPoint(contextRef, 220, 380);
//终点
CGContextAddLineToPoint(contextRef, 240, 380);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

//脚蹬上
CGContextMoveToPoint(contextRef, 210, 332);
//终点
CGContextAddLineToPoint(contextRef, 200, 310);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

CGContextMoveToPoint(contextRef, 190, 310);
//终点
CGContextAddLineToPoint(contextRef, 210, 310);
//画上来 将图形绘制到View上面(渲染)
CGContextSetLineWidth(contextRef, 5);
CGContextStrokePath(contextRef);

}

@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: