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——斐波那契数列