ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

绘制UIBezierPath曲线线条

2021-08-07 18:32:22  阅读:171  来源: 互联网

标签:endP startP UIBezierPath path line 绘制 bezierPath 线条


//线的类型

typedef  NS_ENUM(NSUInteger,BezierPathType){

    kPen_bezierPath = 1,

    kVectorLine_bezierPath,//2

    kArc_bezierPath,//3

    kRect_bezierPath,//4

    kIsoscelesTriangle_bezierPath,//5

    kRightTriangle_bezierPath,//6

    kEraser_bezierPath,//7

    

    kMark_Type,//8

    kPhoto_URL//9

};

//针对 线的Model 定义

@interface JLineModel : JBaseDataModel

 @property(nonatomic,strong) NSNumber* lineColor;//线色

@property(nonatomic,assign) CGFloat lineWidth;//线宽

@property(nonatomic,strong)NSMutableArray *lineArray;//线点坐标数组

@property(nonatomic,strong)UIBezierPath *m_linePath; //从坐标转换成bezierpath 两者存其一,减少计算

@property(nonatomic,assign) BezierPathType  m_bezierPathType;//line type 区别当前的画笔是什么类型的

 

 

 

- (UIBezierPath*)funj_getBezierPath:(JLineModel *)line {

    UIBezierPath *path = nil;

    switch (line.m_bezierPathType) {

        case kPen_bezierPath: case kMark_Type:{

            path = [self funj_drawWithPenSubView:line ];

        }break;

        case kVectorLine_bezierPath:{

            path = [self funj_drawToVectorLine:line];

        }break;

        case kArc_bezierPath:{

            path = [self funj_drawToRectOrArc:line isArc:YES];

        }break;

        case kRect_bezierPath:{

            path = [self funj_drawToRectOrArc:line isArc:NO];

        }break;

        case kIsoscelesTriangle_bezierPath:{

            path = [self funj_drawToTriangle:line issosceles:YES];

        }break;

        case kRightTriangle_bezierPath:{

            path = [self funj_drawToTriangle:line issosceles:NO];

        }break;

        case kEraser_bezierPath:{

            path = [self funj_drawWithPenSubView:line];

            [path strokeWithBlendMode:kCGBlendModeClear alpha:1];

        }break;

        default:

            break;

    }

    return path;

}

 

//通过画笔方式 绘制线条

- (UIBezierPath*)funj_drawWithPenSubView:(JLineModel*)line  {

    UIBezierPath* path = [UIBezierPath bezierPath];

    path.lineCapStyle = kCGLineCapRound;  //线条拐角

    path.lineJoinStyle = kCGLineCapRound;

    

    for (NSUInteger j = 0 ; j < line.lineArray.count; j ++) {

        CGPoint  p   = [line.lineArray[j] CGPointValue];

        p = CGPointMake(p.x* kWidth, p.y * kHeight);

        if (j == 0) {

            [path moveToPoint:p];

        } else {

            CGPoint prePoint = [line.lineArray[j-1] CGPointValue];

            prePoint = CGPointMake(prePoint.x* kWidth, prePoint.y* kHeight);

            CGPoint midP = [self funj_calculateMidPointForPoint:p andPoint:prePoint];

            [path addQuadCurveToPoint:midP controlPoint:prePoint];

        }

    }

    return path;

}

- (CGPoint)funj_calculateMidPointForPoint:(CGPoint)p1 andPoint:(CGPoint)p2 {

    return CGPointMake((p1.x + p2.x) / 2.0, (p1.y + p2.y) / 2.0);

}

//绘制矢量直线

- (UIBezierPath*)funj_drawToVectorLine:(JLineModel*)line {

    UIBezierPath* path =[UIBezierPath bezierPath];;

    CGPoint startP = [[line.lineArray firstObject] CGPointValue];

    CGPoint endP = [[line.lineArray lastObject] CGPointValue];

    startP = CGPointMake(startP.x* kWidth, startP.y* kHeight);

    endP = CGPointMake(endP.x* kWidth, endP.y* kHeight);

    [path moveToPoint: CGPointMake(startP.x, startP.y)];

    [path addLineToPoint:CGPointMake(endP.x, endP.y)];

    return path;

}

//绘制圆形 方形

- (UIBezierPath*)funj_drawToRectOrArc:(JLineModel*)line isArc:(BOOL)isArc{

    UIBezierPath* path =nil;

    CGPoint startP = [[line.lineArray firstObject] CGPointValue];

    CGPoint endP = [[line.lineArray lastObject] CGPointValue];

    startP = CGPointMake(startP.x* kWidth, startP.y* kHeight);

    endP = CGPointMake(endP.x* kWidth, endP.y* kHeight);

    CGFloat width = endP.x - startP.x;

    CGFloat height = endP.y-startP.y ;

    

    if(isArc){

        path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(startP.x, startP.y, width, height)];

    }else{

        path=[UIBezierPath bezierPathWithRect:CGRectMake(startP.x, startP.y,width, height)];

    }

    return path;

}

//绘制三角形

- (UIBezierPath*)funj_drawToTriangle:(JLineModel*)line issosceles:(BOOL)isoscelesTriangle{

    UIBezierPath* path =[UIBezierPath bezierPath];

    

    CGPoint startP = [[line.lineArray firstObject] CGPointValue];

    CGPoint endP = [[line.lineArray lastObject] CGPointValue];

    startP = CGPointMake(startP.x* kWidth, startP.y* kHeight);

    endP = CGPointMake(endP.x* kWidth, endP.y* kHeight);

    if(isoscelesTriangle){

        [path moveToPoint:CGPointMake((endP.x-startP.x)/2 + startP.x, startP.y)];

    }else{

        [path moveToPoint:CGPointMake(startP.x, startP.y)];

    }

    [path addLineToPoint:CGPointMake(startP.x, endP.y)];

    [path addLineToPoint:CGPointMake(endP.x, endP.y)];

    [path closePath];

    

    return path;

}

标签:endP,startP,UIBezierPath,path,line,绘制,bezierPath,线条
来源: https://www.cnblogs.com/jeffreys/p/15112708.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有