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);
}```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355

推荐阅读更多精彩内容

  • Quartz2D Quartz2D的API是纯C语言的 数据类型和函数基本都以CG作为前缀CGContextRef...
    哔哩哔哩智能喵阅读 356评论 0 0
  • Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎。它提供了低...
    ShanJiJi阅读 1,537评论 0 20
  • 美好的一天结束了,为我的好种子浇水施肥。 感恩三个人, 1感恩媛姐为我们开的视频会议。 2感恩张姐对我工作的指导。...
    韩春霞阅读 224评论 0 0
  • 清明,我在异乡 山上,鞭炮声 惊醒了回忆 清明,我在异乡 故乡,艾草香味 飘散到面前 清明,我在异乡 雨,未落 魂...
    酸糖与甜橙阅读 297评论 0 2
  • 我的诗和远方 作者:岠山剑客 我一直在寻找诗意, 所以我一直在走向远方, 我会为看到一朵花开而欣喜, 我会为偶遇一...
    岠山剑客阅读 180评论 6 7