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

1.绘图的常用步骤

①获得设备上下文

CGContextRef ctr = UIGraphicsGetCurrentContext();

②在设备上下文绘图

CGContextMoveToPoint(ctr, 1010);

 CGContextAddLineToPoint(ctr, 5050);

③渲染到相应的View

CGContextFillPath(ctr);


一般来说绘制图形,通常是CGContextAdd*****,而更改某些属性则是CGContextSet*****.

绘制圆弧的时候,通常使用:

CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);

其中参数的设置是以x轴为0,下面为+,上面为-。

[objc]  view plain copy
  1. // Only override drawRect: if you perform custom drawing.  
  2. // An empty implementation adversely affects performance during animation.  
  3. - (void)drawRect:(CGRect)rect {  
  4.     // Drawing code  
  5.     //draw3Tri();  
  6.     //draw4Rect();  
  7.     drawEcplise();  
  8. }  
  9.   
  10. void drawEcplise()  
  11. {  
  12.     CGContextRef ctr = UIGraphicsGetCurrentContext();  
  13.       
  14.   //  CGContextAddEllipseInRect(ctr, CGRectMake(20, 20, 100,200));  
  15.    // CGContextAddArcToPoint(ctr, 10, 10, 50, 100, 100);  
  16.     //CGContextAddArc(ctr, 100, 100, 50, -M_PI_2, M_PI_4, 0);  
  17.       
  18.     CGContextMoveToPoint(ctr, 5050);  
  19.     CGContextAddLineToPoint(ctr, 100100);  
  20.       
  21.     CGContextSetRGBStrokeColor(ctr, 1001);  
  22.     CGContextSetLineWidth(ctr, 5);  
  23.     CGContextStrokePath(ctr);  
  24.       
  25.     CGContextMoveToPoint(ctr, 150150);  
  26.     CGContextAddLineToPoint(ctr, 200200);  
  27.       
  28.     CGContextSetRGBStrokeColor(ctr, 0011);  
  29.     CGContextSetLineWidth(ctr, 2);  
  30.     CGContextStrokePath(ctr);  
  31.       
  32.       
  33. }  
  34.   
  35. void draw4Rect()  
  36. {  
  37.     CGContextRef ctr = UIGraphicsGetCurrentContext();  
  38.       
  39.     CGContextAddRect(ctr, CGRectMake(10108080));  
  40.       
  41.     CGContextFillPath(ctr);  
  42. }  
  43.   
  44. void draw3Tri()  
  45. {  
  46.     //1.获得设备上下文  
  47.     CGContextRef ctr = UIGraphicsGetCurrentContext();  
  48.       
  49.     //2.在设备中绘图  
  50.     CGContextMoveToPoint(ctr, 1010);  
  51.     CGContextAddLineToPoint(ctr, 5050);  
  52.     CGContextAddLineToPoint(ctr, 1000);  
  53.     CGContextAddLineToPoint(ctr, 1010);  
  54.     //3.渲染到view中  
  55.     //CGContextStrokePath(ctr);  
  56.     CGContextFillPath(ctr);  
  57. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值