HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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,100,100)]; //绘制圆形路径 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,100)]; //绘制自带圆角的路径 UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,100) cornerRadius:30]; //指定矩形某一个角加圆角(代码示例为左上角) UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,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,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

大佬总结

以上是大佬教程为你收集整理的UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案全部内容,希望文章能够帮你解决UIBezierPath和CAShapeLayer画直线、CGContextRef画直线两种方案所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。