绘制UIBezierPath曲线线条
作者:互联网
//线的类型
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