- (void)drawLine{
NSLog(@"%@",NSStringFromCGRect(self.bounds));
//1.取得一個(gè)跟View相關(guān)聯(lián)的上下文.
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1.設(shè)置起點(diǎn)
[path moveToPoint:CGPointMake(10, 100)];
//2.1.添加一根線到某個(gè)點(diǎn)
[path addLineToPoint:CGPointMake(200, 20)];
//一個(gè)路徑上面可以畫(huà)多條線
// [path moveToPoint:CGPointMake(10, 150)];
// [path addLineToPoint:CGPointMake(200, 100)];
//把上一條線的終點(diǎn)當(dāng)作是下一條線的起點(diǎn).
[path addLineToPoint:CGPointMake(150, 200)];
//設(shè)置上下文的狀態(tài)
//設(shè)置線的寬度
CGContextSetLineWidth(ctx, 10);
//設(shè)置線的連接樣式
CGContextSetLineJoin(ctx, kCGLineJoinBevel);
//設(shè)置頂角的樣式
CGContextSetLineCap(ctx, kCGLineCapRound);
//設(shè)置線的顏色
[[UIColor greenColor] setStroke];
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的內(nèi)容顯示View fill stroke
CGContextStrokePath(ctx);
}
- (void)drawQuadCurve{
//1.獲取跟View相關(guān)聯(lián)的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//2.1設(shè)置起點(diǎn)
[path moveToPoint:CGPointMake(20, 200)];
//2.2添加一條曲線到某個(gè)點(diǎn).
[path addQuadCurveToPoint:CGPointMake(200, 200) controlPoint:CGPointMake(100, 10)];
CGContextSetLineWidth(ctx, 10);
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的內(nèi)容顯示出來(lái).
CGContextStrokePath(ctx);
}
- (void)drawrect{
//畫(huà)矩形.
//1.獲取上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 100, 100)];
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
[[UIColor yellowColor] set];
//4.把上下文的內(nèi)容顯示
CGContextFillPath(ctx);
}
//cornerRadius:圓角半徑--> 可以畫(huà)圓
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 100, 100) cornerRadius:20];
[path stroke];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 100, 100)];
[[UIColor blueColor] set];
//[path fill];
[path stroke];
//ArcCenter:圓心
//radius:圓的半徑
//startAngle:開(kāi)始角度//起始點(diǎn)圓的最右側(cè).
//endAngle:截至角度
//clockwise:順時(shí)針還是逆時(shí)針 YES:順時(shí)針 NO:逆時(shí)針
CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5 - 10;
CGFloat startA = 0;
CGFloat endA = -M_PI_2;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:NO];
[path addLineToPoint:center];
[[UIColor blueColor] set];
//封閉路徑
//[path closePath];
//fill,會(huì)自動(dòng)把路徑給關(guān)閉
[path fill];
- (void)drawRect:(CGRect)rect {
//1.加載圖片
UIImage *image = [UIImage imageNamed:@"001"];
//繪制出來(lái)的圖片,是保持原來(lái)圖片
// [image drawAtPoint:CGPointZero];
//把圖片填充到這個(gè)rect當(dāng)中.
// [image drawInRect:rect];
//添加裁剪區(qū)域 .把超區(qū)裁剪區(qū)域以外都裁剪掉
// UIRectClip(CGRectMake(0, 0, 50, 50));
// [image drawAsPatternInRect:self.bounds];
[[UIColor blueColor] set];
UIRectFill(CGRectMake(10, 10, 100, 100));
}
- (void)drawText{
NSString *str = @"小碼哥小碼哥小碼哥小碼哥";
//AtPoint:文字所畫(huà)的位置
//withAttributes:描述文字的屬性.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//設(shè)置文字大小
dict[NSFontAttributeName] = [UIFont systemFontOfSize:50];
//設(shè)置文字顏色
dict[NSForegroundColorAttributeName] = [UIColor greenColor];
//設(shè)置描邊寬度
dict[NSStrokeWidthAttributeName] = @2;
//設(shè)置描邊顏色
dict[NSStrokeColorAttributeName] = [UIColor blueColor];
//設(shè)置陰影
NSShadow *shadow = [[NSShadow alloc] init];
//設(shè)置陰影的便宜量
shadow.shadowOffset = CGSizeMake(10, 10);
//設(shè)置陰影顏色
shadow.shadowColor = [UIColor greenColor];
//設(shè)置陰影模糊程序
shadow.shadowBlurRadius = 1;
dict[NSShadowAttributeName] = shadow;
//不會(huì)自動(dòng)換行
[str drawAtPoint:CGPointZero withAttributes:dict];
//會(huì)自動(dòng)換行.
[str drawInRect:self.bounds withAttributes:dict];
}
- (void)drawRect:(CGRect)rect {
//1.獲取跟View相關(guān)聯(lián)的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPath];
//添加橫線
[path moveToPoint:CGPointMake(10, 150)];
[path addLineToPoint:CGPointMake(290, 150)];
//把當(dāng)前的狀態(tài)保存到圖片上下文狀態(tài)棧里.
CGContextSaveGState(ctx);
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 10);
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的內(nèi)容渲染出來(lái).
CGContextStrokePath(ctx);
path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(150, 10)];
[path addLineToPoint:CGPointMake(150, 290)];
//恢復(fù)上下文狀態(tài)棧
CGContextRestoreGState(ctx);
//3.把路徑添加到上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的內(nèi)容渲染出來(lái).
CGContextStrokePath(ctx);
}
- (void)drawRect:(CGRect)rect {
//1.獲得跟View相關(guān)聯(lián)的上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2.描述路徑
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
[[UIColor redColor] set];
//上下文的矩陣操作,必須得要在添加路徑之前做操作.
//平移
CGContextTranslateCTM(ctx, 100, 50);
//縮放 當(dāng)為負(fù)數(shù)為朝向改方向反轉(zhuǎn),Quartz坐標(biāo)系和UIView坐標(biāo)系不一樣顷窒,對(duì)從pdf獲取的圖片需要進(jìn)行處理
CGContextScaleCTM(ctx, 0.5, 0.5);
//旋轉(zhuǎn)
CGContextRotateCTM(ctx, M_PI_4);
//3.把路徑 添加到當(dāng)前上下文
CGContextAddPath(ctx, path.CGPath);
//4.把上下文的內(nèi)容渲染出來(lái).
CGContextFillPath(ctx);
}