iOS開發UI-利用Quartz2D 實現基本繪圖(畫三角形、矩形、圓、圓弧)


1.畫三角形  運行結果如下



1.1具體實現步驟

1.1.1首先新建一個project,然后自定義一個view


1.2代碼

[objc] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. #import "htingShapeView.h"  
  2.   
  3. @implementation htingShapeView  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self) {  
  9.         // Initialization code  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (void)drawRect:(CGRect)rect  
  15. {  
  16. //    draw4Rect();  
  17.     drawTriangle();  
  18. }  
  19.   
  20. /** 
  21.  *  畫四邊形 
  22.  */  
  23. void draw4Rect()  
  24. {  
  25.     // 1.獲得上下文  
  26.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  27.       
  28.     // 2.畫矩形  
  29.     CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));  
  30.       
  31.     // set : 同時設置為實心和空心顏色  
  32.     // setStroke : 設置空心顏色  
  33.     // setFill : 設置實心顏色  
  34.     [[UIColor whiteColor] set];  
  35.       
  36.     //    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);  
  37.       
  38.     // 3.繪制圖形  
  39.     CGContextFillPath(ctx);  
  40. }  
  41.   
  42. /** 
  43.  *  畫三角形 
  44.  */  
  45. void drawTriangle()  
  46. {  
  47.     // 1.獲得上下文  
  48.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  49.       
  50.     // 2.畫三角形  
  51.     CGContextMoveToPoint(ctx, 0, 0);  
  52.     CGContextAddLineToPoint(ctx, 100, 100);  
  53.     CGContextAddLineToPoint(ctx, 150, 80);  
  54.     // 關閉路徑(連接起點和最后一個點)起點和終點連起來  
  55.     CGContextClosePath(ctx);  
  56.       
  57.     //  
  58.     CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);  
  59.       
  60.     // 3.繪制圖形  
  61.     CGContextStrokePath(ctx);  
  62. }  
  63.   
  64. @end  


2.畫矩形運行效果如下


2.1具體實現步驟

2.1.1搭建界面同上

2.1.2代碼

[objc] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. #import "htingShapeView.h"  
  2.   
  3. @implementation htingShapeView  
  4.   
  5. - (id)initWithFrame:(CGRect)frame  
  6. {  
  7.     self = [super initWithFrame:frame];  
  8.     if (self) {  
  9.         // Initialization code  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (void)drawRect:(CGRect)rect  
  15. {  
  16.     draw4Rect();  
  17. //    drawTriangle();  
  18. }  
  19.   
  20. /** 
  21.  *  畫四邊形 
  22.  */  
  23. void draw4Rect()  
  24. {  
  25.     // 1.獲得上下文  
  26.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  27.       
  28.     // 2.畫矩形  
  29.     CGContextAddRect(ctx, CGRectMake(10, 10, 150, 100));  
  30.       
  31.     // set : 同時設置為實心和空心顏色  
  32.     // setStroke : 設置空心顏色  
  33.     // setFill : 設置實心顏色  
  34.     [[UIColor whiteColor] set];  
  35.       
  36.     //    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);  
  37.       
  38.     // 3.繪制圖形  
  39.     CGContextFillPath(ctx);  
  40. }  
  41.   
  42. /** 
  43.  *  畫三角形 
  44.  */  
  45. void drawTriangle()  
  46. {  
  47.     // 1.獲得上下文  
  48.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  49.       
  50.     // 2.畫三角形  
  51.     CGContextMoveToPoint(ctx, 0, 0);  
  52.     CGContextAddLineToPoint(ctx, 100, 100);  
  53.     CGContextAddLineToPoint(ctx, 150, 80);  
  54.     // 關閉路徑(連接起點和最后一個點)起點和終點連起來  
  55.     CGContextClosePath(ctx);  
  56.       
  57.     //  
  58.     CGContextSetRGBStrokeColor(ctx, 0, 1, 0, 1);  
  59.       
  60.     // 3.繪制圖形  
  61.     CGContextStrokePath(ctx);  
  62. }  
  63.   
  64. @end  


3.畫圓  圓弧  等 運行效果如下


3.1代碼實現

[objc] view plain copy 在CODE上查看代碼片派生到我的代碼片
  1. #import "htingCircleView.h"  
  2.   
  3. @implementation htingCircleView  
  4.   
  5.   
  6. - (id)initWithFrame:(CGRect)frame  
  7. {  
  8.     self = [super initWithFrame:frame];  
  9.     if (self) {  
  10.         // Initialization code  
  11.     }  
  12.     return self;  
  13. }  
  14.   
  15. /** 
  16.  *  在view第一次顯示到屏幕上的時候會調用一次 
  17.  */  
  18. - (void)drawRect:(CGRect)rect  
  19. {  
  20. //    drawCircle2();  
  21.       drawCircle();  
  22. }  
  23.   
  24. void drawCircle2()  
  25. {  
  26.     // 1.獲得上下文  
  27.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  28.       
  29.     // 2.畫1/4圓  
  30.     CGContextMoveToPoint(ctx, 100, 100);  
  31.     CGContextAddLineToPoint(ctx, 100, 150);  
  32.     CGContextAddArc(ctx, 100, 100, 50, -M_PI_2, M_PI, 1);  
  33.     CGContextClosePath(ctx); //合並路徑  把起點和終點連起來  
  34.       
  35.     [[UIColor redColor] set];//設置顏色  紅色  
  36.       
  37.     // 3.顯示所繪制的東西   FillPath實心  
  38.     CGContextFillPath(ctx);  
  39.   
  40. }  
  41.   
  42.   
  43. /** 
  44.  *  畫圓弧 
  45.  */  
  46. void drawArc()  
  47. {  
  48.     // 1.獲得上下文  
  49.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  50.       
  51.     // 2.畫圓弧  
  52.     // x\y : 圓心  
  53.     // radius : 半徑  
  54.     // startAngle : 開始角度  
  55.     // endAngle : 結束角度  
  56.     // clockwise : 圓弧的伸展方向(0:順時針, 1:逆時針)  
  57.     //    CGContextAddArc(<#CGContextRef c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)  
  58.     CGContextAddArc(ctx, 100, 100, 50, M_PI_2, M_PI, 0);  
  59.     //    CGContextAddArc(ctx, 100(圓心x), 100(圓心y), 50, M_PI_2, M_PI, 0);  
  60.       
  61.       
  62.     // 3.顯示所繪制的東西  
  63.     CGContextFillPath(ctx); //把繪制的路徑用空心顯示出來  
  64.     //CGContextStrokePath(ctx);畫實心  
  65. }  
  66.   
  67. /** 
  68.  *  畫圓 
  69.  */  
  70. void drawCircle()  
  71. {  
  72.     // 1.獲得上下文  
  73.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  74.       
  75.     // 2.畫圓  
  76.     CGContextAddEllipseInRect(ctx, CGRectMake(50, 10, 100, 100));//(50, 10,是坐標也就是這個圓的位置   100, 100表示寬高都是100  
  77.       
  78.     CGContextSetLineWidth(ctx, 10); //設置線寬畫圓環  
  79.       
  80.     // 3.顯示所繪制的東西  
  81.     CGContextStrokePath(ctx);  
  82. }  
  83.   
  84. @end 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM