美文网首页
drawRect简单封装

drawRect简单封装

作者: CHADHEA | 来源:发表于2017-03-10 11:18 被阅读0次

    #define kBlackColor [UIColor blackColor]

    //.h

    //划线

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint;

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint lineWidth:(CGFloat)width;

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor;

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor;

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width fillColor:(UIColor *)fillColor;

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor;

    //画矩形

    + (void)drawRect:(CGRect)rect;

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width;

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor;

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width fillColor:(UIColor *)fillColor;

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor;

    //画曲线

    //int clockwise  0顺时针  1逆时针,默认是0

    //Angle

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle;

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle lineWidth:(CGFloat)width;

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor;

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise;

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise lineWidth:(CGFloat)width;

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor;

    //point + radius

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius;

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius lineWidth:(CGFloat)width;

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor;

    //画圆

    //双填充

    + (void)drawCircleFrame:(CGRect)frame;

    + (void)drawCircleFrame:(CGRect)frame color:(UIColor *)color;

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius;

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius color:(UIColor *)color;

    //外填充

    //默认线宽为1

    + (void)drawCircleFrame:(CGRect)frame trokeColor:(UIColor *)trokeColor;

    + (void)drawCircleFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth trokeColor:(UIColor *)trokeColor;

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius strokeColor:(UIColor *)strokeColor;

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor;

    //内填充

    + (void)drawCircleFrame:(CGRect)frame fillColor:(UIColor *)fillColor;

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius fillColor:(UIColor *)fillColor;

    //画饼

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle;

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color;

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle  clockwise:(int)clockwise color:(UIColor *)color;

    //字符串

    //默认字体:40 颜色:黑色 位置:屏幕中心

    + (void)drawString:(NSString *)str;

    //location

    + (void)drawString:(NSString *)str center:(CGPoint)center;

    + (void)drawString:(NSString *)str rect:(CGRect)rect;

    //font

    + (void)drawString:(NSString *)str font:(UIFont *)font;

    + (void)drawString:(NSString *)str center:(CGPoint)center font:(UIFont *)font;

    + (void)drawString:(NSString *)str rect:(CGRect)rect font:(UIFont *)font;

    //color

    + (void)drawString:(NSString *)str color:(UIColor *)color;

    + (void)drawString:(NSString *)str center:(CGPoint)center color:(UIColor *)color;

    + (void)drawString:(NSString *)str rect:(CGRect)rect color:(UIColor *)color;

    + (void)drawString:(NSString *)str center:(CGPoint)center font:(UIFont *)font color:(UIColor *)color;

    + (void)drawString:(NSString *)str rect:(CGRect)rect font:(UIFont *)font color:(UIColor *)color;

    //图片

    //默认位置:屏幕中心

    + (void)drawImage:(UIImage *)image;

    + (void)drawImage:(UIImage *)image center:(CGPoint)center;

    + (void)drawImage:(UIImage *)image point:(CGPoint)point;

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect;

    //旋转角度

    //默认中心旋转

    + (void)drawImage:(UIImage *)image center:(CGPoint)center angle:(CGFloat)angle;

    + (void)drawImage:(UIImage *)image point:(CGPoint)point angle:(CGFloat)angle;

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect angle:(CGFloat)angle;

    //设置旋转点

    + (void)drawImage:(UIImage *)image center:(CGPoint)center translate:(CGPoint)translate angle:(CGFloat)angle;

    + (void)drawImage:(UIImage *)image point:(CGPoint)point translate:(CGPoint)translate angle:(CGFloat)angle;

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect translate:(CGPoint)translate angle:(CGFloat)angle;

    //.m

    //划线

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint; {

    [self drawLineMoveToPoint:point addLineToPoint:linePoint lineWidth:1 lineColor:kBlackColor];

    }

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint lineWidth:(CGFloat)width {

    [self drawLineMoveToPoint:point addLineToPoint:linePoint lineWidth:width lineColor:kBlackColor];

    }

    + (void)drawLineMoveToPoint:(CGPoint)point addLineToPoint:(CGPoint)linePoint lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor {

    [self drawLinePointArray:@[kValueFromPoint(point.x, point.y),kValueFromPoint(linePoint.x, linePoint.y)] lineWidth:width strokeColor:lineColor];

    }

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor {

    //获取上下文

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    //配置

    //设置直线的起点

    NSValue  *firstValue = [pointArray firstObject];

    CGPoint  firstPoint = kPointFromValue(firstValue);

    CGContextMoveToPoint(ref, firstPoint.x,firstPoint.y);

    //设置直线的下一个点

    for (int i = 1; i < pointArray.count; i ++) {

    NSValue  *value = pointArray[i];

    CGPoint  point = kPointFromValue(value);

    CGContextAddLineToPoint(ref, point.x, point.y);

    }

    [strokeColor setStroke];

    //设置直线的线宽

    CGContextSetLineWidth(ref, width);

    //设置直线起点的样式

    CGContextSetLineCap(ref, kCGLineCapRound);

    //连接

    CGContextSetLineJoin(ref, kCGLineJoinRound);

    CGContextStrokePath(ref);

    }

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width fillColor:(UIColor *)fillColor {

    //获取上下文

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    //配置

    //设置直线的起点

    NSValue  *firstValue = [pointArray firstObject];

    CGPoint  firstPoint = kPointFromValue(firstValue);

    CGContextMoveToPoint(ref, firstPoint.x,firstPoint.y);

    //设置直线的下一个点

    for (int i = 1; i < pointArray.count; i ++) {

    NSValue  *value = pointArray[i];

    CGPoint  point = kPointFromValue(value);

    CGContextAddLineToPoint(ref, point.x, point.y);

    }

    CGContextClosePath(ref);

    [fillColor setFill];

    //设置直线的线宽

    CGContextSetLineWidth(ref, width);

    //设置直线起点的样式

    CGContextSetLineCap(ref, kCGLineCapRound);

    //连接

    CGContextSetLineJoin(ref, kCGLineJoinRound);

    CGContextFillPath(ref);

    }

    + (void)drawLinePointArray:(NSArray*)pointArray lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor {

    if (pointArray.count < 2) {

    return;

    }

    if (strokeColor) {

    [self drawLinePointArray:pointArray lineWidth:width strokeColor:strokeColor];

    }

    if (fillColor) {

    [self drawLinePointArray:pointArray lineWidth:width fillColor:fillColor];

    }

    }

    //画矩形

    + (void)drawRect:(CGRect)rect {

    [self drawRect:rect lineWidth:1];

    }

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width {

    [self drawRect:rect lineWidth:width strokeColor:kBlackColor];

    }

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor {

    //获取上下文

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextAddRect(ref, rect);

    //设置直线的线宽

    CGContextSetLineWidth(ref, width);

    //设置直线起点的样式

    CGContextSetLineCap(ref, kCGLineCapRound);

    [strokeColor setStroke];

    CGContextStrokePath(ref);

    }

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width fillColor:(UIColor *)fillColor {

    //获取上下文

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextAddRect(ref, rect);

    //设置直线的线宽

    CGContextSetLineWidth(ref, width);

    //设置直线起点的样式

    CGContextSetLineCap(ref, kCGLineCapRound);

    [fillColor setFill];

    CGContextFillPath(ref);

    }

    + (void)drawRect:(CGRect)rect lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor fillColor:(UIColor *)fillColor {

    if (fillColor) {

    [self drawRect:rect lineWidth:width fillColor:fillColor];

    }

    if (strokeColor) {

    [self drawRect:rect lineWidth:width strokeColor:strokeColor];

    }

    }

    //画曲线

    //int clockwise  0顺时针  1逆时针

    //angle

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {

    [self drawCurveCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:0 lineWidth:1 lineColor:kBlackColor];

    }

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle lineWidth:(CGFloat)width {

    [self drawCurveCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:0 lineWidth:width lineColor:kBlackColor];

    }

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor {

    [self drawCurveCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:0 lineWidth:width lineColor:lineColor];

    }

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise {

    [self drawCurveCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise lineWidth:1 lineColor:kBlackColor];

    }

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise lineWidth:(CGFloat)width {

    [self drawCurveCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:clockwise lineWidth:width lineColor:kBlackColor];

    }

    + (void)drawCurveCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(int)clockwise lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor {

    CGContextRef  ref = UIGraphicsGetCurrentContext();

    //int clockwise  0顺时针  1逆时针

    CGContextAddArc(ref, circleCenter.x, circleCenter.y, radius, startAngle, endAngle, clockwise);

    //    CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, CGFloat startAngle, CGFloat endAngle, int clockwise)

    CGContextSetLineWidth(ref, width);

    if (lineColor) {

    [lineColor setStroke];

    }

    CGContextStrokePath(ref);

    }

    //point + radius

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius {

    [self drawCurveWithStartPoint:startPoint focusPoint:focusPoint endPoint:endPoint radius:radius lineWidth:1 lineColor:kBlackColor];

    }

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius lineWidth:(CGFloat)width {

    [self drawCurveWithStartPoint:startPoint focusPoint:focusPoint endPoint:endPoint radius:radius lineWidth:width lineColor:kBlackColor];

    }

    + (void)drawCurveWithStartPoint:(CGPoint)startPoint focusPoint:(CGPoint)focusPoint endPoint:(CGPoint)endPoint radius:(CGFloat)radius lineWidth:(CGFloat)width lineColor:(UIColor *)lineColor {

    //    if (startPoint.x == endPoint.x && startPoint.y == endPoint.y) {

    //        return;

    //    }

    //

    //    CGFloat  f_x = fabs(endPoint.x - startPoint.x);

    //    CGFloat  f_y = fabs(endPoint.y - startPoint.y);

    //    CGFloat  s_d = sqrt(f_x * f_x + f_y * f_y);

    //

    //    CGFloat  f_x_h = f_x * 0.5;

    //    CGFloat  f_y_h = f_y * 0.5;

    //    CGFloat  s_d_h = s_d * 0.5;

    //

    //    CGFloat  D_X = f_x * sqrt(radius * radius - s_d_h * s_d_h) / s_d;

    //

    //    if (s_d_h > radius) {

    //        return;

    //    }

    //

    //

    //    CGPoint  center = CGPointZero;

    //

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

    //

    //        if (startPoint.y < endPoint.y) {

    //

    //            if (clockwise) {

    //

    //                center = CGPointMake(startPoint.x - sqrt(radius * radius - f_y_h * f_y_h) , f_y_h + startPoint.y);

    //            }else {

    //

    //                center = CGPointMake(startPoint.x + sqrt(radius * radius - f_y_h * f_y_h) , f_y_h + startPoint.y);

    //            }

    //

    //

    //        }else {

    //

    //            if (clockwise) {

    //

    //                center = CGPointMake(startPoint.x + sqrt(radius * radius - f_y_h * f_y_h) , f_y_h + startPoint.y);

    //            }else {

    //

    //                center = CGPointMake(startPoint.x - sqrt(radius * radius - f_y_h * f_y_h) , f_y_h + startPoint.y);

    //            }

    //

    //

    //        }

    //    }else if (startPoint.y == endPoint.y) {

    //

    //        if (startPoint.x < endPoint.x) {

    //

    //            if (clockwise) {

    //

    //                center = CGPointMake(startPoint.x + f_x_h ,startPoint.y + sqrt(radius * radius - f_x_h * f_x_h));

    //            }else {

    //

    //                center = CGPointMake(startPoint.x + f_x_h ,startPoint.y - sqrt(radius * radius - f_x_h * f_x_h));

    //            }

    //

    //

    //        }else {

    //

    //            if (clockwise) {

    //

    //                center = CGPointMake(startPoint.x + f_x_h ,startPoint.y - sqrt(radius * radius - f_x_h * f_x_h));

    //            }else {

    //

    //                center = CGPointMake(startPoint.x + f_x_h ,startPoint.y + sqrt(radius * radius - f_x_h * f_x_h));

    //            }

    //        }

    //    }else if (startPoint.x > endPoint.x && startPoint.y > endPoint.y) {

    //

    //        if (clockwise) {

    //

    //            center = CGPointMake(startPoint.x + sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), startPoint.y - f_y_h - D_X);

    //        }else {

    //

    //            center = CGPointMake(startPoint.x - sqrt(radius * radius - sqrt(f_y_h - D_X) * (sqrt(D_X + f_y_h))), startPoint.y - f_y_h + D_X);

    //        }

    //

    //    }else if (startPoint.x > endPoint.x && startPoint.y < endPoint.y) {

    //

    //        if (clockwise) {

    //

    //            center = CGPointMake(endPoint.x - sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), endPoint.y - f_y_h - D_X);

    //        }else {

    //

    //            center = CGPointMake(startPoint.x + sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), startPoint.y + f_y_h + D_X);

    //        }

    //

    //    }else if (startPoint.x < endPoint.x && startPoint.y > endPoint.y) {

    //

    //        if (clockwise) {

    //

    //            center = CGPointMake(endPoint.x + sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), endPoint.y + f_y_h + D_X);

    //

    //        }else {

    //

    //            center = CGPointMake(startPoint.x - sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), startPoint.y - f_y_h - D_X);

    //

    //        }

    //

    //    }else if (startPoint.x < endPoint.x && startPoint.y < startPoint.y) {

    //

    //        if (clockwise) {

    //

    //            center = CGPointMake(endPoint.x + sqrt(radius * radius - sqrt(D_X + f_y_h) * (sqrt(D_X + f_y_h))), endPoint.y - f_y_h - D_X);

    //        }else {

    //

    //            center = CGPointMake(endPoint.x - sqrt(radius * radius - sqrt(f_y_h - D_X) * (sqrt(D_X + f_y_h))), endPoint.y - f_y_h + D_X);

    //        }

    //

    //    }

    CGContextRef  ref = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(ref, startPoint.x, startPoint.y);

    CGContextAddArcToPoint(ref, focusPoint.x, focusPoint.y, endPoint.x, endPoint.y, radius);

    CGContextSetLineWidth(ref, width);

    if (lineColor) {

    [lineColor setStroke];

    }else {

    [kBlackColor setStroke];

    }

    CGContextStrokePath(ref);

    }

    //画圆

    //双填充

    + (void)drawCircleFrame:(CGRect)frame {

    [self drawCircleFrame:frame color:kBlackColor];

    }

    + (void)drawCircleFrame:(CGRect)frame color:(UIColor *)color {

    CGContextRef  ref = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(ref, frame);

    if (color) {

    [color set];

    }

    CGContextFillPath(ref);

    }

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius {

    [self drawCircleFrame:CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2)];

    }

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius color:(UIColor *)color {

    [self drawCircleFrame:CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2) color:color];

    }

    //外填充

    //默认线宽为1

    + (void)drawCircleFrame:(CGRect)frame trokeColor:(UIColor *)trokeColor {

    [self drawCircleFrame:frame lineWidth:1.0 trokeColor:trokeColor];

    }

    + (void)drawCircleFrame:(CGRect)frame lineWidth:(CGFloat)lineWidth trokeColor:(UIColor *)trokeColor {

    CGContextRef  ref = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(ref, frame);

    CGContextSetLineWidth(ref, lineWidth);//线的宽度

    if (trokeColor) {

    [trokeColor setStroke];

    }

    CGContextStrokePath(ref);

    }

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius strokeColor:(UIColor *)strokeColor {

    [self drawCircleFrame:CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2) lineWidth:1 trokeColor:strokeColor];

    }

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius lineWidth:(CGFloat)width strokeColor:(UIColor *)strokeColor {

    [self drawCircleFrame:CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2) lineWidth:width trokeColor:strokeColor];

    }

    //内填充

    + (void)drawCircleFrame:(CGRect)frame fillColor:(UIColor *)fillColor {

    CGContextRef  ref = UIGraphicsGetCurrentContext();

    CGContextAddEllipseInRect(ref, frame);

    if (fillColor) {

    [fillColor setFill];

    }

    CGContextFillPath(ref);

    }

    + (void)drawCircleCenter:(CGPoint)center radius:(CGFloat)radius fillColor:(UIColor *)fillColor {

    [self drawCircleFrame:CGRectMake(center.x - radius, center.y - radius, radius * 2, radius * 2) fillColor:fillColor];

    }

    //画饼

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {

    [self drawCakeCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:0 color:kBlackColor];

    }

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle color:(UIColor *)color {

    [self drawCakeCenter:circleCenter radius:radius startAngle:startAngle endAngle:endAngle clockwise:0 color:color];

    }

    + (void)drawCakeCenter:(CGPoint)circleCenter radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle  clockwise:(int)clockwise color:(UIColor *)color {

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(ref, circleCenter.x, circleCenter.y);

    CGContextAddArc(ref, circleCenter.x, circleCenter.y,radius, startAngle, endAngle, clockwise);

    CGContextClosePath(ref);

    if (color) {

    [color set];

    }

    CGContextFillPath(ref);

    }

    //字符串

    //默认字体:40 颜色:黑色 位置:屏幕中心

    + (void)drawString:(NSString *)str {

    [self drawString:str center:CGPointMake(kSCREEN_WIDTH * 0.5, kSCREEN_HEIGHT * 0.5)];

    }

    //location

    + (void)drawString:(NSString *)str center:(CGPoint)center {

    [self drawString:str center:center font:kFontWithSize_40];

    }

    + (void)drawString:(NSString *)str rect:(CGRect)rect {

    [self drawString:str rect:rect font:kFontWithSize_40];

    }

    //font

    + (void)drawString:(NSString *)str font:(UIFont *)font {

    [self drawString:str center:CGPointMake(kSCREEN_WIDTH * 0.5, kSCREEN_HEIGHT * 0.5) font:font];

    }

    + (void)drawString:(NSString *)str center:(CGPoint)center font:(UIFont *)font {

    [self drawString:str center:center font:font color:kBlueColor];

    }

    + (void)drawString:(NSString *)str rect:(CGRect)rect font:(UIFont *)font {

    [self drawString:str rect:rect font:font color:kBlueColor];

    }

    //color

    + (void)drawString:(NSString *)str color:(UIColor *)color {

    [self drawString:str center:CGPointMake(kSCREEN_WIDTH * 0.5, kSCREEN_HEIGHT * 0.5) color:color];

    }

    + (void)drawString:(NSString *)str center:(CGPoint)center color:(UIColor *)color {

    [self drawString:str center:center font:kFontWithSize_40 color:color];

    }

    + (void)drawString:(NSString *)str rect:(CGRect)rect color:(UIColor *)color {

    [self drawString:str rect:rect font:kFontWithSize_40 color:color];

    }

    + (void)drawString:(NSString *)str center:(CGPoint)center font:(UIFont *)font color:(UIColor *)color {

    CGRect  strRect = [str boundingRectWithSize:CGSizeMake(kSCREEN_WIDTH, kSCREEN_HEIGHT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];

    [str drawAtPoint:CGPointMake(center.x - strRect.size.width * 0.5, center.y - strRect.size.height * 0.5) withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color}];

    }

    + (void)drawString:(NSString *)str rect:(CGRect)rect font:(UIFont *)font color:(UIColor *)color {

    [str drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color}];

    }

    //图片

    + (void)drawImage:(UIImage *)image {

    [self drawImage:image center:CGPointMake(kSCREEN_WIDTH * 0.5, kSCREEN_HEIGHT * 0.5)];

    }

    + (void)drawImage:(UIImage *)image center:(CGPoint)center {

    [self drawImage:image point:CGPointMake(center.x - image.size.width * 0.5, center.y - image.size.height * 0.5)];

    }

    + (void)drawImage:(UIImage *)image point:(CGPoint)point {

    [image drawAtPoint:point];

    }

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect {

    [image drawInRect:rect];

    }

    //旋转角度

    + (void)drawImage:(UIImage *)image center:(CGPoint)center angle:(CGFloat)angle {

    [self drawImage:image center:center translate:CGPointMake(center.x, center.y) angle:angle];

    }

    + (void)drawImage:(UIImage *)image point:(CGPoint)point angle:(CGFloat)angle {

    [self drawImage:image point:point translate:CGPointMake(point.x + image.size.width * 0.5, point.y + image.size.height * 0.5) angle:angle];

    }

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect angle:(CGFloat)angle {

    [self drawImage:image rect:rect translate:CGPointMake(rect.origin.x + rect.size.width * 0.5, rect.origin.y + rect.size.height * 0.5) angle:angle];

    }

    //设置旋转点

    + (void)drawImage:(UIImage *)image center:(CGPoint)center translate:(CGPoint)translate angle:(CGFloat)angle {

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ref);

    //设置旋转点

    CGContextTranslateCTM(ref, translate.x, translate.y);

    //旋转

    CGContextRotateCTM(ref, kAngle(angle));

    [self drawImage:image center:center];

    }

    + (void)drawImage:(UIImage *)image point:(CGPoint)point translate:(CGPoint)translate angle:(CGFloat)angle {

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ref);

    //设置旋转点

    CGContextTranslateCTM(ref, translate.x, translate.y);

    //旋转

    CGContextRotateCTM(ref, kAngle(angle));

    [self drawImage:image point:point];

    }

    + (void)drawImage:(UIImage *)image rect:(CGRect)rect translate:(CGPoint)translate angle:(CGFloat)angle {

    CGContextRef    ref = UIGraphicsGetCurrentContext();

    CGContextSaveGState(ref);

    //设置旋转点

    CGContextTranslateCTM(ref, translate.x, translate.y);

    //旋转

    CGContextRotateCTM(ref, kAngle(angle));

    [self drawImage:image rect:rect];

    }

    谢谢阅读!

    相关文章

      网友评论

          本文标题:drawRect简单封装

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