日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案

發(fā)布時(shí)間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在view的相關(guān)方法中可直接使用UIBezierPath和CAShapeLayer畫圖形
- (void)makeBezierPath {/**CAShapeLayer屬于QuartzCore框架,繼承自CALayer。CAShapeLayer是在坐標(biāo)系內(nèi)繪制貝塞爾曲線的,通過繪制貝塞爾曲線,設(shè)置shape(形狀)的path(路徑),從而繪制各種各樣的圖形以及不規(guī)則圖形。因此,使用CAShapeLayer需要與UIBezierPath一起使用。UIBezierPath對(duì)象(貝塞爾曲線),它是CGPathRef數(shù)據(jù)類型的封裝CAShapeLayer是一個(gè)通過矢量圖形而不是bitmap來繪制的圖層子類。相對(duì)于Core Graphics繪制圖片,使用CAShapeLayer有以下一些優(yōu)點(diǎn):1、渲染快速。CAShapeLayer使用了硬件加速(使用CPU渲染),繪制同一圖形會(huì)比用Core Graphics快很多2、高效使用內(nèi)存。一個(gè)CAShapeLayer不需要像普通CALayer一樣創(chuàng)建一個(gè)寄宿圖形,所以無論有多大,都不會(huì)占用太多的內(nèi)存3、不會(huì)被圖層邊界剪裁掉。一個(gè)CAShapeLayer可以在邊界之外繪制。*/// 線的路徑UIBezierPath *linePath = [UIBezierPath bezierPath];// 起點(diǎn)[linePath moveToPoint:CGPointMake(100, 100)];// 其他點(diǎn)[linePath addLineToPoint:CGPointMake(160, 160)];[linePath addLineToPoint:CGPointMake(180, 120)];CAShapeLayer *lineLayer = [CAShapeLayer layer];lineLayer.lineWidth = 2;lineLayer.strokeColor = [UIColor greenColor].CGColor;lineLayer.path = linePath.CGPath;lineLayer.fillColor = nil; // 默認(rèn)為blackColor [self.view.layer addSublayer:lineLayer];/**//繪制矩形UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];//繪制圓形路徑UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];//繪制自帶圓角的路徑UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:30];//指定矩形某一個(gè)角加圓角(代碼示例為左上角)UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];*/ }
CGContextRef畫圖只能在view的drawRect方法中,drawRect方法系統(tǒng)會(huì)默認(rèn)創(chuàng)建一個(gè)上下文 -(void)drawRect:(CGRect)rect {[self makeLine];[self makeLineTwo]; } - (void)makeLine {//注意,在drawRect方法中系統(tǒng)會(huì)默認(rèn)創(chuàng)建一個(gè)上下文(C語言類型)//下面這個(gè)方法中的rect參數(shù)會(huì)傳入當(dāng)前view的frame//Graphics Context是圖形上下文,可以將其理解為一塊畫布CGContextRef context = UIGraphicsGetCurrentContext();//創(chuàng)建路徑CGMutablePathRef path = CGPathCreateMutable();//設(shè)置起點(diǎn)CGPathMoveToPoint(path, NULL, 50, 150);//設(shè)置終點(diǎn)CGPathAddLineToPoint(path, NULL, 100, 150);//顏色 [[UIColor redColor] setStroke];//線寬CGContextSetLineWidth(context, 5.0);//設(shè)置連接樣式 CGContextSetLineJoin(context, kCGLineJoinBevel);//設(shè)置頂角樣式 CGContextSetLineCap(context, kCGLineCapRound);//3、把路徑添加到上下文 CGContextAddPath(context, path);//4、渲染上下文到View的layer CGContextStrokePath(context); } - (void)makeLineTwo {//1、獲取圖形上下文CGContextRef ctx = UIGraphicsGetCurrentContext();//2、描述路徑(底層封裝路徑)CGContextMoveToPoint(ctx, 200, 200);CGContextAddLineToPoint(ctx , 250, 250);[[UIColor orangeColor] setStroke];//3、渲染上下文到View的layer CGContextStrokePath(ctx); }

https://www.jianshu.com/p/139f4fbe7b6b

https://www.jianshu.com/p/a9d39a4946a5

https://www.cnblogs.com/jaesun/p/iOS-CAShapeLayerUIBezierPath-hua-xian.html

轉(zhuǎn)載于:https://www.cnblogs.com/lulushen/p/11163965.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。