參考文章:https://bihongbo.com/2016/01/03/memoryGhostdrawRect/#more
? ? ? ? ? ? ? ? ? ? http://www.cocoachina.com/ios/20170809/20187.html
? ??????????????????https://zsisme.gitbooks.io/ios-/content/chapter2/custom-drawing.html
iOS提供了兩套繪圖框架,分別是UIBezierPath和Core Graphics奶赔。UIBezierPath屬于UIKit。UIBezierPath是對(duì)Core Graphics框架的進(jìn)一步封裝。
OpenGL和Core Graphics都是繪圖專用的API類族凯旋,調(diào)用圖形處理器(GPU)進(jìn)行圖形的繪制和渲染他嫡。在架構(gòu)上是平級(jí)的颖医,相比UIkit更接近底層枉长。
CALayer 和 UIView區(qū)別
實(shí)際上你所看到的視圖內(nèi)容冀续,包括圖形等,都是由UIView的一個(gè)實(shí)例圖層屬性來繪制和渲染的必峰,那就是CALayer洪唐。
CALayer類的概念與UIView非常類似,它也具有樹形的層級(jí)關(guān)系吼蚁,并且可以包含圖片文本凭需、背景色等。它與UIView最大的不同在于它不能響應(yīng)用戶交互桂敛,可以說它根本就不知道響應(yīng)鏈的存在功炮,它的API雖然提供了“某點(diǎn)是否在圖層范圍內(nèi)的方法”,但是它并不具有響應(yīng)的能力术唬。
在每一個(gè)UIView實(shí)例當(dāng)中薪伏,都有一個(gè)默認(rèn)的支持圖層,UIView負(fù)責(zé)創(chuàng)建并且管理這個(gè)圖層粗仓。實(shí)際上這個(gè)CALayer圖層才是真正用來在屏幕上顯示的嫁怀,UIView僅僅是對(duì)它的一層封裝,實(shí)現(xiàn)了CALayer的delegate借浊,提供了處理事件交互的具體功能塘淑,還有動(dòng)畫底層方法的高級(jí)API÷旖铮可以說CALayer是UIView的內(nèi)部實(shí)現(xiàn)細(xì)節(jié)存捺。
1.CAShapeLayer +UIBezierPath
-(void)drawMyLayer{
? ? CGSize size=[UIScreen mainScreen].bounds.size;
? ? CGFloatWIDTH =20.0;
? ? CALayer*layer;
? ? CAShapeLayer* brownRectLayer = [CAShapeLayer layer];
? ? brownRectLayer.frame=CGRectMake(0,0,100,100);
? ? UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0,100, 100)];
? ? brownRectLayer.path= path.CGPath;
? ? brownRectLayer.fillColor= [UIColorbrownColor].CGColor;
? ? [self.view.layeraddSublayer:brownRectLayer];
}
效果:
context:圖形上下文,可以通過UIGraphicsGetCurrentContext:獲取當(dāng)前視圖的上下文
imageContext:圖片上下文曙蒸,可以通過UIGraphicsBeginImageContextWithOptions:獲取一個(gè)圖片上下文捌治,然后繪制完成后,調(diào)用UIGraphicsGetImageFromCurrentImageContext獲取繪制的圖片纽窟,最后要記得關(guān)閉圖片上下文UIGraphicsEndImageContext肖油。
//圖片合并
-(UIImage*)createLabel{
? ? //開啟圖形上下文
? ? NSString*text =@"123456";
? ? UIImage*image = [UIImageimageNamed:@"icon_60pt"];
? ? UIGraphicsBeginImageContextWithOptions(image.size, NO,1.0);
? ? //將圖片繪制到
? ? [imagedrawInRect:CGRectMake(0,0,image.size.width, image.size.height)];
? ? NSDictionary*attr =@{
?? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFontboldSystemFontOfSize:20],? //設(shè)置字體 ? ? ? ? ? ? ? ? ? ? ? ? ? ????????NSForegroundColorAttributeName: [UIColorredColor]? ? ? //設(shè)置字體顏色 ? ? ? ? ? ? ? ? ? ? ??
? ? };
? ? [textdrawAtPoint:CGPointMake(20, 20) withAttributes:attr];
? ? UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
? ? UIGraphicsEndImageContext();
? ? returnnewImage;
}
?//獲得根圖層
? ? layer=[[CALayeralloc]init];
? ? //設(shè)置背景顏色,由于QuartzCore是跨平臺(tái)框架,無法直接使用UIColor
? ? layer.backgroundColor=[UIColor colorWithRed:0 green:146/255.0 blue:1.0 alpha:1.0].CGColor;
? ? //設(shè)置中心點(diǎn)
? ? layer.position=CGPointMake(size.width/2, size.height/2);
? ? //設(shè)置大小
? ? layer.bounds=CGRectMake(0,0, WIDTH,WIDTH);
? ? //設(shè)置圓角,當(dāng)圓角半徑等于矩形的一半時(shí)看起來就是一個(gè)圓形
? ? layer.cornerRadius=WIDTH/2;
? ? //設(shè)置陰影
? ? layer.shadowColor=[UIColor grayColor].CGColor;
? ? layer.shadowOffset=CGSizeMake(2,2);
? ? layer.shadowOpacity=.9;
? ? //設(shè)置邊框
? ? //? ? layer.borderColor=[UIColor whiteColor].CGColor;
? ? //? ? layer.borderWidth=1;
? ? //設(shè)置錨點(diǎn)
? ? //? ? layer.anchorPoint=CGPointZero;
? ? [self.view.layeraddSublayer:layer];
- (void)drawRect:(CGRect)rect {
? ? //獲取圖形上下文
? ? CGContextRef ctx = UIGraphicsGetCurrentContext();
? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);
? ? CGContextSetLineWidth(ctx,2.0);
? ? CGContextMoveToPoint(ctx,80,30);
? ? CGContextAddLineToPoint(ctx,80,150);
? ? CGContextStrokePath(ctx);
? ? CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
? ? CGContextMoveToPoint(ctx,100,30);
? ? CGContextAddLineToPoint(ctx,100,100);
? ? CGContextStrokePath(ctx);
? ? CGContextMoveToPoint(ctx,120,30);
? ? CGContextAddLineToPoint(ctx,120,100);
? ? CGContextStrokePath(ctx);
? ? CGContextMoveToPoint(ctx,150,30);
? ? CGContextAddLineToPoint(ctx,190,30);
? ? CGContextStrokePath(ctx);
? ? //CGContextAddRect(ctx, CGRectMake(0,0,100,100));
? ? CGContextFillRect(ctx, CGRectMake(0,0,1000,1000));
? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);;
? ? CGContextStrokePath(ctx);
? ? /*
? ? [[UIColor orangeColor] set];
? ? UIBezierPath *path = [UIBezierPath bezierPath];
? ? path.lineWidth = 2.0;
? ? [path moveToPoint:CGPointMake(80,100)];
? ? [path? addLineToPoint:CGPointMake(80,150)];
? ? [path stroke];
? ? UIBezierPath *cirlePaht = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(80-10,150,20,20)];
? ? [[UIColor grayColor]set];
? ? cirlePaht.lineWidth = 5/3;
? ? [cirlePaht stroke];
? ? [cirlePaht fill];
? ? UIBezierPath *paths = [UIBezierPath bezierPath];
? ? paths.lineWidth = 2.0;
? ? [paths moveToPoint:CGPointMake(80,170)];
? ? [paths? addLineToPoint:CGPointMake(80,200)];
? ? [paths stroke];
? ? */
}
- (void)drawRect:(CGRect)rect {
? ? //獲取圖形上下文
? ? /*
? ? CGContextRef ctx = UIGraphicsGetCurrentContext();
? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);
? ? CGContextSetLineWidth(ctx,2.0);
? ? CGContextMoveToPoint(ctx,80,30);
? ? CGContextAddLineToPoint(ctx,80,150);
? ? CGContextStrokePath(ctx);
? ? CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
? ? CGContextMoveToPoint(ctx,100,30);
? ? CGContextAddLineToPoint(ctx,100,100);
? ? CGContextStrokePath(ctx);
? ? CGContextMoveToPoint(ctx,120,30);
? ? CGContextAddLineToPoint(ctx,120,100);
? ? CGContextStrokePath(ctx);
? ? CGContextMoveToPoint(ctx,150,30);
? ? CGContextAddLineToPoint(ctx,190,30);
? ? CGContextStrokePath(ctx);
? ? //CGContextAddRect(ctx, CGRectMake(0,0,100,100));
? ? CGContextFillRect(ctx, CGRectMake(0,0,100,100));
? ? CGContextSetStrokeColorWithColor(ctx,[UIColor redColor].CGColor);;
? ? CGContextStrokePath(ctx);
? ? */
? ? [[UIColor orangeColor] set];
? ? UIBezierPath *path = [UIBezierPath bezierPath];
? ? path.lineWidth=2.0;
? ? [pathmoveToPoint:CGPointMake(80,100)];
? ? [path? addLineToPoint:CGPointMake(80,150)];
? ? [pathstroke];
? ? UIBezierPath *cirlePaht = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(80-10,150,20,20)];
? ? [[UIColor grayColor]set];
? ? cirlePaht.lineWidth=5/3;
? ? [cirlePahtstroke];
? ? [cirlePahtfill];
? ? UIBezierPath *paths = [UIBezierPath bezierPath];
? ? paths.lineWidth=2.0;
? ? [pathsmoveToPoint:CGPointMake(80,170)];
? ? [paths? addLineToPoint:CGPointMake(80,200)];
? ? [pathsstroke];
}