標籤:
- 利用CG架構繪圖的步驟:
- 1.子類化UIView
- 2.在GraphicView中複寫drawRect方法
- 3.擷取圖形內容物件
- 4.繪製線條
- (1)先建立一個路徑,把線條定義在這個路徑上,再把路徑交給context去顯示。
- (2)直接把線條繪製到context上去。
- 5.繪製簡單圖形
- 6.繪製文本
10. 7.繪製映像
12.- (void)drawRect:(CGRect)rect {
13. //1.擷取圖形上下文
- CGContextRef context = UIGraphicsGetCurrentContext();//圖形上下文
- [self drawImage:context];
17.}
19.////繪製線條方法一
20.//2.建立一個繪製路徑
- CGMutablePathRef path = CGPathCreateMutable();
22.//在路徑上添加一個起點
- CGPathMoveToPoint(path, NULL, 50, 50);
- CGPathAddLineToPoint(path, NULL, 100, 100);
25.//繪製完畢後關閉路徑,這樣會把起始點和終點串連
- CGPathCloseSubpath(path);
27.//3.把路徑添加到圖形上下文
- CGContextAddPath(context, path);
29.//4.設定圖形內容相關的屬性
- //設定線寬
- CGContextSetLineWidth(context, 5);
- //設定線條顏色
- CGContextSetRGBStrokeColor(context, 254.0/255, 201.0/255, 21.0/255, 1);
- //填充顏色
- CGContextSetRGBFillColor(context, 0, 1, 0, 1);
36.//5.在圖形上下文上繪製路徑
- /*
- 繪製模式:
- kCGPathFill:填充(實心)
- kCGPathStroke:畫線(空心)
- kCGPathFillStroke :即畫線又填充
- */
- CGContextDrawPath(context, kCGPathFillStroke);
44.//6.釋放路徑,有create就要有release
- CGPathRelease(path);
47.////繪製線條方法二
48.CGPoint points[] = {{50,50},{100,100},{50,100},{50,50}};
- CGContextAddLines(context, points, 4);
50.//設定線條顏色
- [[UIColor redColor] setStroke];
52.//設定填充顏色
- [[UIColor blueColor] setFill];
54.//同時設定線條和填充顏色
- [[UIColor redColor] set];
56.CGContextDrawPath(context, kCGPathFillStroke);
58.//繪製矩形
59.//1.使用CG提供的函數
- CGRect rect = CGRectMake(10, 10, 100, 200);
- //添加到圖形上下文中
- CGContextAddRect(context, rect);
- //設定context的屬性
- [[UIColor redColor] set];
- CGContextDrawPath(context, kCGPathFillStroke);
66.//2.使用UIKit架構提供的函數
- CGRect rect = CGRectMake(10, 10, 100, 200);
- //設定context的屬性
- [[UIColor redColor] set];
- //繪製實心矩形
- //UIRectFill(rect);
- //繪製空心矩形
- UIRectFrame(rect);
76.//繪製一個圓形
77.//1.
- x,y圓心的座標
- radius:半徑
- startAngel:起始角度
- endAngel:結束的角度
- clockwise:0順時針,1逆時針
- CGContextAddArc(context, 160, 160, 100, 0, M_PI_4, 0);
84.//2.給定一個矩形,繪製一個內切圓
- CGRect rect = CGRectMake(50, 50, 100, 100);
- [[UIColor redColor] set];
- UIRectFrame(rect);
- CGContextAddEllipseInRect(context, rect);
- CGContextDrawPath(context, kCGPathFillStroke);
91.//繪製貝賽爾曲線
92.//設定起點
- CGContextMoveToPoint(context, 20, 200);
- //CGContextAddCurveToPoint(context, 100, 20, 200, 300, 300, 50);
- CGContextAddQuadCurveToPoint(context, 150, 20, 300, 200);
- [[UIColor redColor] set];
- CGContextDrawPath(context, kCGPathStroke);
98.//繪製一個文本
- NSString *str = @"hello world";
-
- CGRect rect = CGRectMake(50, 50, 200, 300);
-
- [[UIColor whiteColor] setFill];
-
- UIRectFill(rect);
-
- UIFont *font = [UIFont systemFontOfSize:20];
- UIColor *color = [UIColor redColor];
-
- //建立段落樣式對象,通過對其屬性的設定可以設定文字在段落中顯示的樣式。
- NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
- style.lineBreakMode = NSLineBreakByWordWrapping;
- style.alignment = NSTextAlignmentCenter;
-
- // [str drawInRect:rect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
-
- [str drawInRect:rect withAttributes:
- @{
- NSFontAttributeName:font,
- NSForegroundColorAttributeName:color,
- NSParagraphStyleAttributeName:style
- }];
- //這種繪製文本的方式非常輕量級,不需要建立一個UILabel控制項,並且可以根據需求定製效果。
-
- //繪製圖片
- //1.使用UIKit提供的方法繪製
- UIImage *image = [UIImage imageNamed:@"mp3.jpg"];
- 圖片展開在指定矩形地區顯示
- [image drawInRect:CGRectMake(0, 0, 320, 200)];
- 圖片平鋪在指定地區顯示
- [image drawAsPatternInRect:CGRectMake(0, 0, 320, 200)];
- //2.用CoreGraphic提供的函數繪製
- //CG座標系統轉化為UIKit的座標系統
- CGContextSaveGState(context);
- //(1)順時針旋轉180度
- CGContextRotateCTM(context, M_PI);
- //(2)x縮放為原來的-1倍
- CGContextScaleCTM(context, -1, 1);
- //(3)向上平移
- CGContextTranslateCTM(context, 0, -image.size.height);
- CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
- //(4)恢複context
- CGContextRestoreGState(context);
Quartz 2D簡單的繪圖