美文网首页
绘图工具类

绘图工具类

作者: ZZ_军哥 | 来源:发表于2020-05-19 16:33 被阅读0次

    总结:大部分绘图时,由于每个需求不一样,分享一点绘图必用的工具类,多次实践,简单实用.

    使用方法:将需要绘图的View继承这个类,就可调用里面的方法.

    涵盖内容:折线,曲线,直线,圆形,文字绘制,数值点转View的点位等内容.

    ```

    @interface GYJDrawSuperView : UIView

    typedef enum

    {

        //点处于文字的那个位置

        TextLocationAtLeftTop,//左上角

        TextLocationAtLeftBottom,//左下角

        TextLocationAtRightTop,//右上角

        TextLocationAtRightBottom,//右下角

        TextLocationAtCenter,//居中

        TextLocationAtCenterUp,//上方中间

        TextLocationAtCenterDown,//下方中间

        TextLocationAtleftCenter,//左边中间

        TextLocationAtRightCenter,//右边中间

    }TextLocationType;

    typedef enum

    {

        CommonDerectionType,//X轴从左到右数字变大 或 Y轴从下到上变大

        ReverseDerectionType,//X轴从左到右数字变小 或 Y轴从下到上变小

    }AxisDerectionType;

    typedef enum

    {

        //_哪根轴_往轴的那个方向_分割线处于轴的哪边

        GYJSeparated_X_Left_Up,  //X轴往左分割,分割线处于X轴上方

        GYJSeparated_X_Left_Down,  //X轴往左分割,分割线处于X轴下方

        GYJSeparated_X_Right_Up,  //X轴往右分割,分割线处于X轴上方

        GYJSeparated_X_Right_Down,  //X轴往右分割,分割线处于X轴下方

        GYJSeparated_Y_Up_Left,  //Y轴往上分割,分割线处于Y轴左边

        GYJSeparated_Y_Up_Right,  //Y轴往上分割,分割线处于Y轴右边

        GYJSeparated_Y_Down_Left,  //Y轴往下分割,分割线处于Y轴左边

        GYJSeparated_Y_Down_Right,  //Y轴往下分割,分割线处于Y轴右边

    }GYJSeparatedType;

    #pragma mark- *************************** 直线(虚线)  ***************************

    /**

     绘制直线

     @param startPoint 起点

     @paramendPoint终点

     @paramlineWidth线宽

     @paramisDottedLine是否是虚线

     @paramlineColor线的颜色

     */

    - (void)GYJ_drawLineWithStartPoint:(CGPoint)startPoint andEndPoint:(CGPoint)endPoint andLineWidth:(CGFloat) lineWidth andIsDottedLine:(BOOL)isDottedLine andLineColor:(UIColor *)lineColor;

    #pragma mark- *************************** 圆形  ***************************

    /**

     绘制圆

     @param centerPoint 圆心

     @paramradius半径

     @paramcycleColor线条颜色

     @paramisFill是否实心绘制/空心

     */

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointandRadius:(CGFloat)radiusandColor:(UIColor*)cycleColorandIsFill:(BOOL)isFill;

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointandRadius:(CGFloat)radiusandStrokeColor:(UIColor*)strokeColorandFillColor:(UIColor*)fillColor;

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointradius:(CGFloat)radiuslineWidth:(CGFloat)lineWidthstokeColor:(UIColor*)stokeColorfillColor:(UIColor*)fillColor;

    #pragma mark- *************************** 绘制文字  ***************************

    /**

     绘制文字(文字位置由textType类型决定)

     @paramtextStr文本内容

     @paramtextFont字号

     @paramtextColor字体颜色

     @paramtextTypecurrentPoint处于文字的哪个位置

     @param currentPoint 基础点

     */

    - (void)GYJ_drawTextWithText:(NSString *)textStr andTextFont:(CGFloat)textFont andTextColor:(UIColor *)textColor andTextLocationType:(TextLocationType)textType andCurrentPoint:(CGPoint)currentPoint;

    #pragma mark- *************************** 原始点转换为视图上的点  ***************************

    /**

     将正常状况下的坐标系的后台的点数组转换为视图中的点数组

     注意:此方法在正常坐标系下为前提

     @paramoriginalPoints原始点的数组(后台数据) [NSValue valueWithCGPoint:CGPoint] 类型

     @paramzeroPoint视图中坐标点为(0,0)的点

     @paramxScale原始X点/视图X点 缩放系数

     @paramyScale原始Y点/视图Y点 缩放系数

     @return 返回转换成视图中的点数组

     */

    - (NSArray <NSValue *>*)GYJ_changeOriginalPointsToViewPointsAtDeafaultDerection:(NSArray <NSValue *>*)originalPoints viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale;

    /**

     根据特殊坐标系转换视图中的点

     @paramoriginalPoints原始点的数组(后台数据) [NSValue valueWithCGPoint:CGPoint] 类型

     @paramzeroPoint视图中坐标点为(0,0)的点

     @paramxScale原始X点/视图X点 缩放系数

     @paramyScale原始Y点/视图Y点 缩放系数

     @paramxTypeCommonDerectionType(X轴从左到右数字变大)  ReverseDerectionType(X轴从左到右数字变小)

     @paramyTypeCommonDerectionType(Y轴从下到上变大)  ReverseDerectionType(Y轴从下到上变小)

     @return 返回转换成视图中的点数组

     */

    - (NSArray <NSValue *>*)GYJ_changeOriginalPointsToViewPoints:(NSArray <NSValue *>*)originalPoints viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale xAxisDerectionType:(AxisDerectionType)xType YaxisDerectionType:(AxisDerectionType)yType;

    /**

     单个点转换

     @paramoriginalPoint原始点的数组(后台数据) [NSValue valueWithCGPoint:CGPoint] 类型

     @paramzeroPoint视图中坐标点为(0,0)的点

     @paramxScale原始X点/视图X点 缩放系数

     @paramyScale原始Y点/视图Y点 缩放系数

     @paramxTypeCommonDerectionType(X轴从左到右数字变大)  ReverseDerectionType(X轴从左到右数字变小)

     @paramyTypeCommonDerectionType(Y轴从下到上变大)  ReverseDerectionType(Y轴从下到上变小)

     @return 返回转换成视图中的点

     */

    - (CGPoint)GYJ_changeOrignalPointToViewPoint:(CGPoint)originalPoint viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale XaxisDerectionType:(AxisDerectionType)xType YaxisDerectionType:(AxisDerectionType)yType;

    #pragma mark- *************************** 射线(角度)  ***************************

    /**

     根据X轴的起点和终点的原始数据,与X轴的夹角 计算出另一个原始点 (正切)

     注意:startPoint 和 endPoint的Y值相同 正切

     @paramstartPoint射线起点 如(800m,200英尺)

     @paramendPoint射线终点

     @paramangle射线与水平线的夹角 (直接给角度,会自动转换为弧度)

     @paramratioScale原始Y点/视图Y点 X轴对比Y轴的换算系数

     @paramisbigger射线的Y值是比endPoint的是否大  (区别夹角的上下方)

     @return 返回该角度的另一个点

     */

    - (CGPoint)GYJ_changeRayLineWithXAsixStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint tanAngle:(CGFloat)angle ratioXToYScale:(CGFloat)ratioScale isBigger:(BOOL)isbigger;

    /**

     根据X的起点和终点的原始数据,与X轴的夹角 计算出另一个原始点 (余切)

     注意:startPoint 和 endPoint的Y值相同  余切

     @paramstartPoint射线起点 如(800m,200英尺)

     @paramendPoint射线终点 (Y值和射线起点的Y值相同的点)

     @paramangle射线与水平线的夹角 (直接给角度,会自动转换为弧度)

     @paramratioScale原始Y点/视图Y点 X轴对比Y轴的换算系数

     @paramisbigger射线的X值是比endPoint的是否大  (区别夹角的上下方)

     @return 余切的另一个点

     */

    - (CGPoint)GYJ_changeRayLineWithXAsixStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint cotAngle:(CGFloat)angle ratioXToYScale:(CGFloat)ratioScale isBigger:(BOOL)isbigger;

    #pragma mark- *************************** 绘制长方形  ***************************

    /**

     绘制长方形 (可填充)

     @paramviewRect长方形的frame

     @paramlineWidth外线框线宽

     @paramfillColor填充色

     @paramstrokeColor线条颜色

     */

    - (void)GYJ_drawRectViewWithRect:(CGRect)viewRectlineWidth:(CGFloat)lineWidthfillColor:(UIColor*)fillColorstrokeColor:(UIColor*)strokeColor;

    #pragma mark- *************************** 绘制折线/平滑  ***************************

    /**

     绘制折线(可将拐点进行画圆放大处理)

     @parampointsArray折线点数组

     @paramlineWidth线宽

     @paramlineColor线条颜色

     @paramisNeedEnlarge是否将转折点放大处理

     @parampointsRaduis放大转折点的半径

     @parampointsColor放大点的颜色

     */

    - (UIBezierPath*)GYJ_drawPieceLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor  isNeedEnlargePoints:(BOOL)isNeedEnlargepointsRadius:(CGFloat)pointsRaduispointsColor:(UIColor*)pointsColor;

    /**

     绘制折线(纯粹)

     @parampointsArray折线点数组

     @paramlineWidth线宽

     @paramlineColor线条颜色

     */

    - (UIBezierPath*)GYJ_drawPieceLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor;

    //折线(虚线)

    - (void)GYJ_drawPieceDottedLineTogether:(NSArray *)pointsArr lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor;

    /**

     绘制平滑曲线(平滑曲线)

     @parampointsArray折线点数组

     @paramlineWidth线宽

     @paramlineColor线条颜色

     */

    - (UIBezierPath*)GYJ_drawPieceSmoothLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor;

    #pragma mark- *************************** 绘制分割线(多条平行的线)  ***************************

    /**

     绘制分割线

     @paramstartPoint分割线的起点

     @paramseperateType确定是往X或Y的那个方向分割

     @paramaverageMargin每份之间的间距

     @paramseperateNum分割多少份

     @parampointsMargin分割线起点到终点的长度

     @paramlineColor线条颜色

     @paramlineWidth线宽

     @paramaddWidth将返回的点进行延长多少个点

     @return 分割线终点的点数组

     */

    - (NSArray*)GYJ_drawSeparatedWithStartPoint:(CGPoint)startPointseperateType:(GYJSeparatedType)seperateTypeaverageMargin:(CGFloat)averageMarginseperateNum:(NSInteger)seperateNumstartEndPointMargin:(CGFloat)pointsMarginlineColor:(UIColor*)lineColorlineWidth:(CGFloat)lineWidthaddWidth:(CGFloat)addWidth;

    @end

    ```

    ```

    #import "GYJDrawSuperView.h"

    @implementation GYJDrawSuperView

    //根据传入的起点和终点进行绘制直线

    - (void)GYJ_drawLineWithStartPoint:(CGPoint)startPoint andEndPoint:(CGPoint)endPoint andLineWidth:(CGFloat) lineWidth andIsDottedLine:(BOOL)isDottedLine andLineColor:(UIColor *)lineColor

    {

        UIBezierPath *linePath = [UIBezierPath bezierPath];

        linePath.lineWidth= lineWidth;

        [linePathmoveToPoint:startPoint];

        [linePathaddLineToPoint:endPoint];

        [lineColorsetStroke];

        if(isDottedLine ==YES) {//是否虚线

            CGFloatlengths[] = {3,3};

            [linePathsetLineDash:lengthscount:2phase:0];

        }

        [linePathstroke];

    }

    //绘制小圆圈

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointandRadius:(CGFloat)radiusandColor:(UIColor*)cycleColorandIsFill:(BOOL)isFill

    {

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];

        if(isFill){

            [cycleColorsetFill];

            [pathfill];

        }else{

            [cycleColorsetStroke];

            [pathstroke];

        }

    }

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointandRadius:(CGFloat)radiusandStrokeColor:(UIColor*)strokeColorandFillColor:(UIColor*)fillColor

    {

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];

        [strokeColorsetStroke];

        [fillColorsetFill];

        [pathstroke];

        [pathfill];

    }

    - (void)GYJ_drawCycleWithPoint:(CGPoint)centerPointradius:(CGFloat)radiuslineWidth:(CGFloat)lineWidthstokeColor:(UIColor*)stokeColorfillColor:(UIColor*)fillColor

    {

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:centerPoint radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];

        path.lineWidth= lineWidth;

        [stokeColorsetStroke];

        [fillColorsetFill];

        [pathstroke];

        [pathfill];

    }

    - (void)GYJ_drawTextWithText:(NSString *)textStr andTextFont:(CGFloat)textFont andTextColor:(UIColor *)textColor andTextLocationType:(TextLocationType)textType andCurrentPoint:(CGPoint)currentPoint

    {

        CGSize textSize = [textStr sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:textFont],NSForegroundColorAttributeName:textColor}];

        CGPoint startPoint = [self GYJ_calculateTextLeftTopPointWithTextSize:textSize andTextLocationType:textType andCurrentPoint:currentPoint];

        [textStrdrawInRect:CGRectMake(startPoint.x, startPoint.y, textSize.width, textSize.height) withAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:textFont],NSForegroundColorAttributeName:textColor}];

    }

    //计算左上角的点

    - (CGPoint)GYJ_calculateTextLeftTopPointWithTextSize:(CGSize)textSize andTextLocationType:(TextLocationType)textType andCurrentPoint:(CGPoint)currentPoint

    {

        switch(textType) {

            case TextLocationAtLeftTop:

                returncurrentPoint;

            case TextLocationAtLeftBottom:

                returnCGPointMake(currentPoint.x, currentPoint.y+textSize.height);

            case TextLocationAtRightTop:

                returnCGPointMake(currentPoint.x-textSize.width, currentPoint.y);

            case TextLocationAtRightBottom:

                returnCGPointMake(currentPoint.x-textSize.width, currentPoint.y-textSize.height);

            case TextLocationAtCenter:

                returnCGPointMake(currentPoint.x-textSize.width/2, currentPoint.y-textSize.height/2);

            case TextLocationAtCenterUp:

                returnCGPointMake(currentPoint.x-textSize.width/2, currentPoint.y);

            case TextLocationAtCenterDown:

                returnCGPointMake(currentPoint.x-textSize.width/2, currentPoint.y-textSize.height/2);

            case TextLocationAtleftCenter:

                returnCGPointMake(currentPoint.x, currentPoint.y-textSize.height/2);

            case TextLocationAtRightCenter:

                returnCGPointMake(currentPoint.x-textSize.width, currentPoint.y-textSize.height/2);

            default:

                returnCGPointMake(0,0);//

        }

    }

    //将正常状况下的点数组转换为视图中的点数组

    - (NSArray <NSValue *>*)GYJ_changeOriginalPointsToViewPointsAtDeafaultDerection:(NSArray <NSValue *>*)originalPoints viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale

    {

        return [self GYJ_changeOriginalPointsToViewPoints:originalPoints viewZeroPoint:zeroPoint XScale:xScale YScale:yScale xAxisDerectionType:CommonDerectionType YaxisDerectionType:CommonDerectionType];

    }

    //将原始的点数组转换为视图上的点数组

    - (NSArray <NSValue *>*)GYJ_changeOriginalPointsToViewPoints:(NSArray <NSValue *>*)originalPoints viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale xAxisDerectionType:(AxisDerectionType)xType YaxisDerectionType:(AxisDerectionType)yType

    {

        NSMutableArray *tempArray = [NSMutableArray array];

        for(NSIntegeri =0; i

            CGPointcurrentPoint = [originalPoints[i]CGPointValue];

            CGPoint finallyPoint = [self GYJ_changeOrignalPointToViewPoint:currentPoint viewZeroPoint:zeroPoint XScale:xScale YScale:yScale XaxisDerectionType:xType YaxisDerectionType:yType];

            [tempArrayaddObject:[NSValuevalueWithCGPoint:finallyPoint]];

        }

        returntempArray.copy;

    }

    //将原始的点转换为视图上的点

    - (CGPoint)GYJ_changeOrignalPointToViewPoint:(CGPoint)originalPoint viewZeroPoint:(CGPoint)zeroPoint XScale:(CGFloat)xScale YScale:(CGFloat)yScale XaxisDerectionType:(AxisDerectionType)xType YaxisDerectionType:(AxisDerectionType)yType

    {

        //得到转换后的X轴坐标

        CGFloatfinallyX =0.0f;

        if (xType == CommonDerectionType) {

            finallyX = zeroPoint.x+ originalPoint.x* xScale;

        }else if(xType == ReverseDerectionType){

            finallyX = zeroPoint.x- originalPoint.x* xScale;

        }

        //处理转换后的Y轴坐标

        CGFloatfinallyY =0.0f;

        if (yType == CommonDerectionType) {

            finallyY = zeroPoint.y- originalPoint.y* yScale;

        }else if(yType == ReverseDerectionType){

            finallyY = zeroPoint.y+ originalPoint.y* yScale;

        }

        returnCGPointMake(finallyX, finallyY);

    }

    //根据2个原始点 计算出另外一个与X轴成一定夹角的末端点

    - (CGPoint)GYJ_changeRayLineWithXAsixStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint tanAngle:(CGFloat)angle ratioXToYScale:(CGFloat)ratioScale isBigger:(BOOL)isbigger

    {

        if(startPoint.y!= endPoint.y) {

            NSLog(@"射线的起点和终点需保证在同一水平线上");

            returnCGPointMake(0,0);

        }

        //计算两点间的距离 fabs(a,b)

        CGFloatPointsMargin = startPoint.x- endPoint.x;//计算两点间的差

        CGFloatxWidth = PointsMargin>0? PointsMargin : -PointsMargin;//计算X轴的两点间的间距

        CGFloatyHeight = xWidth *tan(angle/180*M_PI)*ratioScale;//转换后Y轴的数字

        CGFloatY = isbigger ==YES? (endPoint.y+yHeight) : (endPoint.y-yHeight);

        returnCGPointMake(endPoint.x, Y);

    }

    - (CGPoint)GYJ_changeRayLineWithXAsixStartPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint cotAngle:(CGFloat)angle ratioXToYScale:(CGFloat)ratioScale isBigger:(BOOL)isbigger

    {

        if(startPoint.x!= endPoint.x) {

            NSLog(@"射线的起点和终点需保证在同一水平线上");

            returnCGPointMake(0,0);

        }

        //计算两点间的距离 fabs(a,b)

        CGFloatPointsMargin = startPoint.y- endPoint.y;//计算两点间的差

        CGFloatyHeight = PointsMargin>0? PointsMargin : -PointsMargin;//计算X轴的两点间的间距

        CGFloatxWidth = yHeight /(tan(angle/180*M_PI)*ratioScale);//转换后Y轴的数字

        CGFloatX = isbigger ==YES? (endPoint.x+xWidth) : (endPoint.x-xWidth);

        returnCGPointMake(X, endPoint.y);

    }

    //绘制长方形

    - (void)GYJ_drawRectViewWithRect:(CGRect)viewRectlineWidth:(CGFloat)lineWidthfillColor:(UIColor*)fillColorstrokeColor:(UIColor*)strokeColor

    {

        UIBezierPath *path = [UIBezierPath bezierPathWithRect:viewRect];

        path.lineWidth= lineWidth;

        [strokeColorsetStroke];

        [pathstroke];

        if(fillColor ==nil) {

            return;

        }

        [fillColorsetFill];

        [pathfill];

    }

    //绘制连线

    - (UIBezierPath*)GYJ_drawPieceLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor  isNeedEnlargePoints:(BOOL)isNeedEnlargepointsRadius:(CGFloat)pointsRaduispointsColor:(UIColor*)pointsColorisNeedSmooth:(BOOL)isNeedSmooth

    {

        //绘制折线

        UIBezierPath *path = [UIBezierPath bezierPath];

        for(NSIntegeri =0; i

            CGPointcurrentPoint = [pointsArray[i]CGPointValue];

            if(i ==0) {

                [pathmoveToPoint:currentPoint];

            }else{

                [pathaddLineToPoint:currentPoint];

            }

            if(isNeedEnlarge ==YES) {

                UIBezierPath *pointPath = [UIBezierPath bezierPathWithArcCenter:currentPoint radius:pointsRaduis startAngle:0 endAngle:M_PI*2 clockwise:YES];

                [pointsColorsetFill];

                [pointPathfill];

            }

        }

        path.lineWidth= lineWidth;

        if(isNeedSmooth) {

            path = [pathsmoothedPathWithGranularity:100];//将折线平滑处理

        }

        [lineColorsetStroke];

        [pathstroke];

        //绘制圆点

        if(isNeedEnlarge ==YES) {

            for(NSIntegeri =0; i

                CGPointcurrentPoint = [pointsArray[i]CGPointValue];

                UIBezierPath *pointPath = [UIBezierPath bezierPathWithArcCenter:currentPoint radius:pointsRaduis startAngle:0 endAngle:M_PI*2 clockwise:YES];

                [pointsColorsetFill];

                [pointPathfill];

            }

        }

        returnpath;

    }

    //绘制折线 (点 经过画圆处理)

    - (UIBezierPath*)GYJ_drawPieceLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor  isNeedEnlargePoints:(BOOL)isNeedEnlargepointsRadius:(CGFloat)pointsRaduispointsColor:(UIColor*)pointsColor

    {

        return [self GYJ_drawPieceLineTogether:pointsArray lineWidth:lineWidth lineColor:lineColor isNeedEnlargePoints:isNeedEnlarge pointsRadius:pointsRaduis pointsColor:pointsColor isNeedSmooth:NO];

    }

    //纯粹折线

    - (UIBezierPath*)GYJ_drawPieceLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor

    {

        return [self GYJ_drawPieceLineTogether:pointsArray lineWidth:lineWidth lineColor:lineColor isNeedEnlargePoints:NO pointsRadius:0 pointsColor:nil];

    }

    //折线(虚线)

    - (void)GYJ_drawPieceDottedLineTogether:(NSArray *)pointsArr lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor

    {

        UIBezierPath *path = [UIBezierPath bezierPath];

        for(NSIntegeri =0; i

            CGPointcurrentPoint = [pointsArr[i]CGPointValue];

            if(i==0) {

                [pathmoveToPoint:currentPoint];

            }else{

                [pathaddLineToPoint:currentPoint];

            }

        }

        path.lineWidth= lineWidth;

        [lineColorsetStroke];

        CGFloatlengths[] = {3,3};

        [pathsetLineDash:lengthscount:2phase:0];

        [pathstroke];

    }

    //平滑的曲线

    - (UIBezierPath*)GYJ_drawPieceSmoothLineTogether:(NSArray*)pointsArraylineWidth:(CGFloat)lineWidthlineColor:(UIColor*)lineColor

    {

        return [self GYJ_drawPieceLineTogether:pointsArray lineWidth:lineWidth lineColor:lineColor isNeedEnlargePoints:NO pointsRadius:0 pointsColor:nil isNeedSmooth:YES];

    }

    //绘制分割线

    - (NSArray*)GYJ_drawSeparatedWithStartPoint:(CGPoint)startPointseperateType:(GYJSeparatedType)seperateTypeaverageMargin:(CGFloat)averageMarginseperateNum:(NSInteger)seperateNumstartEndPointMargin:(CGFloat)pointsMarginlineColor:(UIColor*)lineColorlineWidth:(CGFloat)lineWidthaddWidth:(CGFloat)addWidth

    {

        NSMutableArray *endPointsArr = [NSMutableArray array];//保存修改后的终点

        for(NSIntegeri =0; i

            CGFloatreallyMargin = averageMargin * i;

            CGPointaxisNextPoint = [selfGYJ_dealWithStartPoint:startPointdoublePointsMargin:reallyMarginseperateType:seperateType];//得到分割线的起点

            CGPointseperateEndPoint = [selfGYJ_dealWithStartPoint:axisNextPointstartEndPointMargin:pointsMarginseperateType:seperateType];//得到分割线的终点

            [self GYJ_drawLineWithStartPoint:axisNextPoint andEndPoint:seperateEndPoint andLineWidth:lineWidth andIsDottedLine:NO andLineColor:lineColor];//绘制直线

            CGPointnewPoint = [selfGYJ_dealWithStartPoint:seperateEndPointstartEndPointMargin:addWidthseperateType:seperateType];//得到添加一段距离后的终点

            [endPointsArraddObject:[NSValuevalueWithCGPoint:newPoint]];

        }

        returnendPointsArr.copy;

    }

    //轴方向分割线之间点与点处理

    - (CGPoint)GYJ_dealWithStartPoint:(CGPoint)startPoint doublePointsMargin:(CGFloat)averageMargin seperateType:(GYJSeparatedType)seperateType

    {

        CGFloatX = startPoint.x;

        CGFloatY = startPoint.y;

        switch(seperateType) {

            case GYJSeparated_X_Left_Up:

                X -= averageMargin;

                break;

            case GYJSeparated_X_Left_Down:

                X -= averageMargin;

                break;

            case GYJSeparated_X_Right_Up:

                X += averageMargin;

                break;

            case GYJSeparated_X_Right_Down:

                X += averageMargin;

                break;

            case GYJSeparated_Y_Up_Left:

                Y -= averageMargin;

                break;

            case GYJSeparated_Y_Up_Right:

                Y -= averageMargin;

                break;

            case GYJSeparated_Y_Down_Left:

                Y += averageMargin;

                break;

            case GYJSeparated_Y_Down_Right:

                Y += averageMargin;

                break;

        }

        returnCGPointMake(X, Y);

    }

    //分割线起点和终点的处理

    - (CGPoint)GYJ_dealWithStartPoint:(CGPoint)startPoint startEndPointMargin:(CGFloat)pointsMargin seperateType:(GYJSeparatedType)seperateType

    {

        CGFloatX = startPoint.x;

        CGFloatY = startPoint.y;

        switch(seperateType) {

            case GYJSeparated_X_Left_Up:

                Y -= pointsMargin;

                break;

            case GYJSeparated_X_Left_Down:

                Y += pointsMargin;

                break;

            case GYJSeparated_X_Right_Up:

                Y -= pointsMargin;

                break;

            case GYJSeparated_X_Right_Down:

                Y += pointsMargin;

                break;

            case GYJSeparated_Y_Up_Left:

                X -= pointsMargin;

                break;

            case GYJSeparated_Y_Up_Right:

                X += pointsMargin;

                break;

            case GYJSeparated_Y_Down_Left:

                X -= pointsMargin;

                break;

            case GYJSeparated_Y_Down_Right:

                X += pointsMargin;

                break;

        }

        returnCGPointMake(X, Y);

    }

    @end

    ```

    曲线化的类:

    相关文章

      网友评论

          本文标题:绘图工具类

          本文链接:https://www.haomeiwen.com/subject/olrshctx.html