iOS 绘制图形

绘制几种常见图形的类方法。

绘制图形.png

1.绘制圆形

/*
 *画圆
 *context   当前上下文
 *fillColor 填充色
 *radius    半径
 *point     圆心点坐标
 */
+ (void)drawCircle:(CGContextRef)context
         fillcolor:(UIColor *)fillColor
            radius:(CGFloat)radius
             point:(CGPoint)point {
    CGContextSaveGState(context);
    CGContextSetShouldAntialias(context, YES);
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    CGContextAddArc(context, point.x, point.y, radius, 0, M_PI * 2, 0);
    CGContextDrawPath(context, kCGPathFill);
    CGContextRestoreGState(context);
}

2.绘制同心圆

/*
 *画同心圆
 *context   当前上下文
 *lineColor 外圆颜色
 *fillColor 内圆颜色
 *lineWidth 外圆半径-内圆半径
 *radius    内圆半径
 *point     圆心点坐标
 */
+ (void)drawConcentricCircle:(nullable CGContextRef)context
                   lineColor:(nullable UIColor *)lineColor
                   fillColor:(nullable UIColor *)fillColor
                   lineWidth:(CGFloat)lineWidth
                      radius:(CGFloat)radius
                       point:(CGPoint)point {
    CGContextSetShouldAntialias(context, YES);
    CGContextSetLineWidth(context, lineWidth);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);   //线的填充色
    CGContextSetFillColorWithColor(context, fillColor.CGColor);     //圆的填充色
    
    //内圆
    CGContextAddArc(context, point.x, point.y, radius, 0, M_PI * 2, 0);
    CGContextDrawPath(context, kCGPathFill);
    
    //外圆
    CGContextAddArc(context, point.x, point.y, radius, 0, M_PI * 2, 0);
    CGContextDrawPath(context, kCGPathStroke);
}

3.绘制矩形

/*
 *画矩形
 *context   当前上下文
 *color     填充色
 *rect      矩形位置的大小
 */
+ (void)drawRect:(nullable CGContextRef)context
           color:(nullable UIColor*)color
            rect:(CGRect)rect {
    CGContextSetShouldAntialias(context, YES);
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextAddRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
}

4.绘制圆角矩形

/*
 *context       当前上下文
 *lineColor     边框颜色
 *fillColor     填充颜色
 *lineWidth     边框宽度
 *cornerRadius  圆角半径
 *rect          矩形位置和大小
 */
+ (void)drawRoundedRect:(nullable CGContextRef)context
              lineColor:(nullable UIColor *)lineColor
              fillColor:(nullable UIColor *)fillColor
              lineWidth:(CGFloat)lineWidth
           cornerRadius:(CGFloat)cornerRadius
                   rect:(CGRect)rect {
    CGFloat x = rect.origin.x;
    CGFloat y = rect.origin.y;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    
    CGContextSetShouldAntialias(context, YES);
    CGContextSetLineWidth(context, lineWidth);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    
    CGContextMoveToPoint(context, x + width, y + height - cornerRadius);    //从右下角开始画起
    CGContextAddArcToPoint(context, x + width, y + height, x + width - cornerRadius, y + height, cornerRadius); //右下角的圆角
    CGContextAddArcToPoint(context, x, y + height, x, y + height - cornerRadius, cornerRadius);                 //左下角的圆角
    CGContextAddArcToPoint(context, x, y, x + cornerRadius, y, cornerRadius);                                   //左上角的圆角
    CGContextAddArcToPoint(context, x + width, y, x + width, y + height - cornerRadius, cornerRadius);          //右上角的圆角
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
}

5.绘制三角形

typedef NS_ENUM(NSInteger, TriangleDirection) {
    TriangleDirectionUp = 0,    //向上
    TriangleDirectionDown,      //向下
    TriangleDirectionLeft,      //向左
    TriangleDirectionRight      //向右
};
#pragma mark - 绘制三角形
/*
 *画正三角形
 *context       当前上下文
 *lineColor     边框颜色
 *fillColor     填充颜色
 *centerPoint   正三角形中心
 *length        边长
 *lineWidth     边框宽度
 *TriangleDirection 三角形方向
 */
+ (void)drawTriangle:(nullable CGContextRef)context
           lineColor:(nullable UIColor *)lineColor
           fillColor:(nullable UIColor *)fillColor
         centerPoint:(CGPoint)centerPoint
              length:(CGFloat)length
           lineWidth:(CGFloat)lineWidth
           direction:(TriangleDirection)direction {
    CGContextSetShouldAntialias(context, YES);
    CGContextSetLineWidth(context, lineWidth);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    //正三角形的外接圆半径
    CGFloat radius = length * sqrt(3) / 3.0;
    //正三角形的圆心距离底边的距离
    CGFloat distance = length * sqrt(3) / 6.0;
    CGFloat halfLength = length / 2.0;
    CGPoint points[3];
    switch (direction) {
        case TriangleDirectionUp:
            points[0] = CGPointMake(centerPoint.x, centerPoint.y - radius);
            points[1] = CGPointMake(centerPoint.x - halfLength, centerPoint.y + distance);
            points[2] = CGPointMake(centerPoint.x + halfLength, centerPoint.y + distance);
            break;
        case TriangleDirectionDown:
            points[0] = CGPointMake(centerPoint.x, centerPoint.y + radius);
            points[1] = CGPointMake(centerPoint.x - halfLength, centerPoint.y - distance);
            points[2] = CGPointMake(centerPoint.x + halfLength, centerPoint.y - distance);
            break;
        case TriangleDirectionLeft:
            points[0] = CGPointMake(centerPoint.x - radius, centerPoint.y);
            points[1] = CGPointMake(centerPoint.x + distance, centerPoint.y - halfLength);
            points[2] = CGPointMake(centerPoint.x + distance, centerPoint.y + halfLength);
            break;
        case TriangleDirectionRight:
            points[0] = CGPointMake(centerPoint.x + radius, centerPoint.y);
            points[1] = CGPointMake(centerPoint.x - distance, centerPoint.y - halfLength);
            points[2] = CGPointMake(centerPoint.x - distance, centerPoint.y + halfLength);
            break;
        default:
            break;
    }
    
    CGContextAddLines(context, points, 3);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    CGContextDrawPath(context, kCGPathStroke);
}

/*
 *任意三角形
 *context       当前上下文
 *lineColor     边框颜色
 *fillColor     填充颜色
 *pointArr      三个点的坐标
 *lineWidth     边框宽度
 */
+ (void)drawTriangle:(nullable CGContextRef)context
           lineColor:(nullable UIColor *)lineColor
           fillColor:(nullable UIColor *)fillColor
            pointArr:(nullable NSArray *)pointArr
           lineWidth:(CGFloat)lineWidth {
    
    CGContextSaveGState(context);
    CGContextSetShouldAntialias(context, YES);
    CGContextSetLineWidth(context, lineWidth);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetFillColorWithColor(context, fillColor.CGColor);
    
    if (pointArr.count != 3) {
        return;
    }
    CGPoint points[3];
    points[0] = [pointArr[0] CGPointValue];
    points[1] = [pointArr[1] CGPointValue];
    points[2] = [pointArr[2] CGPointValue];
    
    CGContextAddLines(context, points, 3);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
    CGContextRestoreGState(context);
}


作者:Jixin
链接:https://www.jianshu.com/p/96a24bc5d483
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2020-06-19 11:00  brave-sailor  阅读(513)  评论(0编辑  收藏  举报