UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案
生活随笔
收集整理的這篇文章主要介紹了
UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在view的相關方法中可直接使用UIBezierPath和CAShapeLayer畫圖形
- (void)makeBezierPath {/**CAShapeLayer屬于QuartzCore框架,繼承自CALayer。CAShapeLayer是在坐標系內繪制貝塞爾曲線的,通過繪制貝塞爾曲線,設置shape(形狀)的path(路徑),從而繪制各種各樣的圖形以及不規則圖形。因此,使用CAShapeLayer需要與UIBezierPath一起使用。UIBezierPath對象(貝塞爾曲線),它是CGPathRef數據類型的封裝CAShapeLayer是一個通過矢量圖形而不是bitmap來繪制的圖層子類。相對于Core Graphics繪制圖片,使用CAShapeLayer有以下一些優點:1、渲染快速。CAShapeLayer使用了硬件加速(使用CPU渲染),繪制同一圖形會比用Core Graphics快很多2、高效使用內存。一個CAShapeLayer不需要像普通CALayer一樣創建一個寄宿圖形,所以無論有多大,都不會占用太多的內存3、不會被圖層邊界剪裁掉。一個CAShapeLayer可以在邊界之外繪制。*/// 線的路徑UIBezierPath *linePath = [UIBezierPath bezierPath];// 起點[linePath moveToPoint:CGPointMake(100, 100)];// 其他點[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; // 默認為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];//指定矩形某一個角加圓角(代碼示例為左上角)UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];*/ }
CGContextRef畫圖只能在view的drawRect方法中,drawRect方法系統會默認創建一個上下文 -(void)drawRect:(CGRect)rect {[self makeLine];[self makeLineTwo]; } - (void)makeLine {//注意,在drawRect方法中系統會默認創建一個上下文(C語言類型)//下面這個方法中的rect參數會傳入當前view的frame//Graphics Context是圖形上下文,可以將其理解為一塊畫布CGContextRef context = UIGraphicsGetCurrentContext();//創建路徑CGMutablePathRef path = CGPathCreateMutable();//設置起點CGPathMoveToPoint(path, NULL, 50, 150);//設置終點CGPathAddLineToPoint(path, NULL, 100, 150);//顏色 [[UIColor redColor] setStroke];//線寬CGContextSetLineWidth(context, 5.0);//設置連接樣式 CGContextSetLineJoin(context, kCGLineJoinBevel);//設置頂角樣式 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); }
- (void)makeBezierPath {/**CAShapeLayer屬于QuartzCore框架,繼承自CALayer。CAShapeLayer是在坐標系內繪制貝塞爾曲線的,通過繪制貝塞爾曲線,設置shape(形狀)的path(路徑),從而繪制各種各樣的圖形以及不規則圖形。因此,使用CAShapeLayer需要與UIBezierPath一起使用。UIBezierPath對象(貝塞爾曲線),它是CGPathRef數據類型的封裝CAShapeLayer是一個通過矢量圖形而不是bitmap來繪制的圖層子類。相對于Core Graphics繪制圖片,使用CAShapeLayer有以下一些優點:1、渲染快速。CAShapeLayer使用了硬件加速(使用CPU渲染),繪制同一圖形會比用Core Graphics快很多2、高效使用內存。一個CAShapeLayer不需要像普通CALayer一樣創建一個寄宿圖形,所以無論有多大,都不會占用太多的內存3、不會被圖層邊界剪裁掉。一個CAShapeLayer可以在邊界之外繪制。*/// 線的路徑UIBezierPath *linePath = [UIBezierPath bezierPath];// 起點[linePath moveToPoint:CGPointMake(100, 100)];// 其他點[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; // 默認為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];//指定矩形某一個角加圓角(代碼示例為左上角)UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(50, 50)];*/ }
CGContextRef畫圖只能在view的drawRect方法中,drawRect方法系統會默認創建一個上下文 -(void)drawRect:(CGRect)rect {[self makeLine];[self makeLineTwo]; } - (void)makeLine {//注意,在drawRect方法中系統會默認創建一個上下文(C語言類型)//下面這個方法中的rect參數會傳入當前view的frame//Graphics Context是圖形上下文,可以將其理解為一塊畫布CGContextRef context = UIGraphicsGetCurrentContext();//創建路徑CGMutablePathRef path = CGPathCreateMutable();//設置起點CGPathMoveToPoint(path, NULL, 50, 150);//設置終點CGPathAddLineToPoint(path, NULL, 100, 150);//顏色 [[UIColor redColor] setStroke];//線寬CGContextSetLineWidth(context, 5.0);//設置連接樣式 CGContextSetLineJoin(context, kCGLineJoinBevel);//設置頂角樣式 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
轉載于:https://www.cnblogs.com/lulushen/p/11163965.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 『转载』在vs2008(2005)win
- 下一篇: 剑指Offer——斐波那契数列