IOS绘制小黄人,对绘图知识的一个练习
作者:互联网
IOS绘制小黄人,对绘图知识的一个练习
#define kTopX rect.size.width * 0.5
#define kTopRadius 80
#define kTopY 260
//
// HMView.m
// 004-绘制小黄人
//
// Created by lujun on 2021/6/11.
//
#import "HMView.h"
@implementation HMView
- (void)drawRect:(CGRect)rect{
CGContextRef ctx = UIGraphicsGetCurrentContext();
//画身体
[self drawBody:rect withContext:ctx];
[self drawEyes:rect withContext:ctx];
[self drawMouth:rect withContext:ctx];
[self drawhair:rect withContext:ctx];
}
-(void)drawhair:(CGRect)rect withContext:(CGContextRef) ctx{
CGContextMoveToPoint(ctx, kTopX-30, kTopY-kTopRadius-30);
CGContextAddLineToPoint(ctx, kTopX-20, kTopY-kTopRadius+5 );
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, kTopX-22, kTopY-kTopRadius-30);
CGContextAddLineToPoint(ctx, kTopX-8, kTopY-kTopRadius+5 );
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, kTopX, kTopY-kTopRadius-30);
CGContextAddLineToPoint(ctx, kTopX, kTopY-kTopRadius+5 );
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
CGContextMoveToPoint(ctx, kTopX+30, kTopY-kTopRadius-30);
CGContextAddLineToPoint(ctx, kTopX+20, kTopY-kTopRadius+5 );
[[UIColor blackColor] set];
CGContextMoveToPoint(ctx, kTopX+22, kTopY-kTopRadius-30);
CGContextAddLineToPoint(ctx, kTopX+8, kTopY-kTopRadius+5 );
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
}
-(void)drawMouth:(CGRect)rect withContext:(CGContextRef) ctx{
//贝塞尔曲线
//控制点
CGFloat controllerX = kTopX;
CGFloat controllerY = rect.size.height*0.3 + 50;
//当前点
CGFloat marginX = 20;
CGFloat marginY = 10;
CGFloat currentX = controllerX-marginX;
CGFloat currentY = controllerY - marginY;
CGContextMoveToPoint(ctx, currentX, currentY);
//结束点
CGFloat endX = controllerX + marginX;
CGFloat endY = currentY;
CGContextAddQuadCurveToPoint(ctx, controllerX, controllerY, endX, endY);
CGContextSetLineWidth(ctx, 1);
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
}
-(void)drawEyes:(CGRect)rect withContext:(CGContextRef) ctx{
CGFloat startX = kTopX - kTopRadius;
CGFloat startY = kTopY;
CGContextMoveToPoint(ctx, startX, startY);
CGFloat endX = kTopX + kTopRadius;
CGFloat endY = startY;
CGContextAddLineToPoint(ctx, endX, endY);
CGContextSetLineWidth(ctx, 18);
[[UIColor blackColor] set];
CGContextStrokePath(ctx);
//画左边第一支大眼睛
CGFloat leftRaduis = 30;
CGFloat leftX = kTopX - leftRaduis;
CGFloat leftY = kTopY;
CGContextAddArc(ctx, leftX, leftY, leftRaduis, 0, 2 * M_PI, 0);
[[UIColor grayColor] set];
CGContextFillPath(ctx);
//画右边第一支大眼睛
CGFloat rightRaduis = 30;
CGFloat rightX = kTopX + leftRaduis;
CGFloat rightY = kTopY;
CGContextAddArc(ctx, rightX, rightY, rightRaduis, 0, 2 * M_PI, 0);
[[UIColor grayColor] set];
CGContextFillPath(ctx);
//画左边二眼睛
CGFloat leftSmallRaduis = 20;
CGFloat leftSmallX = leftX;
CGFloat leftSmallY = leftY;
CGContextAddArc(ctx, leftSmallX, leftSmallY, leftSmallRaduis, 0, 2 * M_PI, 0);
[[UIColor whiteColor] set];
CGContextFillPath(ctx);
//画右边二眼睛
CGFloat rightSmallRaduis = 20;
CGFloat rightSmallX = rightX;
CGFloat rightSmallY = rightY;
CGContextAddArc(ctx, rightSmallX, rightSmallY, rightSmallRaduis, 0, 2 * M_PI, 0);
[[UIColor whiteColor] set];
CGContextFillPath(ctx);
//画左边三眼睛
CGFloat leftSsmallRaduis = 8;
CGFloat leftSsmallX = leftX +8;
CGFloat leftSsmallY = leftY;
CGContextAddArc(ctx, leftSsmallX, leftSsmallY, leftSsmallRaduis, 0, 2 * M_PI, 0);
[[UIColor colorWithRed:88/255.0 green:23/255.0 blue:9/255.0 alpha:1] set];
CGContextFillPath(ctx);
//画右边三眼睛
CGFloat rightSsmallRaduis = 8;
CGFloat rightSsmallX = rightX -8;
CGFloat rightSsmallY = rightY;
CGContextAddArc(ctx, rightSsmallX, rightSsmallY, rightSsmallRaduis, 0, 2 * M_PI, 0);
[[UIColor colorWithRed:88/255.0 green:23/255.0 blue:9/255.0 alpha:1] set];
CGContextFillPath(ctx);
//画左边四眼睛
CGContextAddArc(ctx, leftSsmallX, leftSsmallY, 4, 0, 2 * M_PI, 0);
[[UIColor blackColor] set];
CGContextFillPath(ctx);
//画右边四眼睛
CGContextAddArc(ctx, rightSsmallX, rightSsmallY, 4, 0, 2 * M_PI, 0);
[[UIColor blackColor] set];
CGContextFillPath(ctx);
//画左边五眼睛,白点
CGContextAddArc(ctx, leftSsmallX-3, leftSsmallY-3, 2, 0, 2 * M_PI, 0);
[[UIColor whiteColor] set];
CGContextFillPath(ctx);
//画右边五眼睛,白点
CGContextAddArc(ctx, rightSsmallX-3, rightSsmallY-3, 2, 0, 2 * M_PI, 0);
[[UIColor whiteColor] set];
CGContextFillPath(ctx);
}
-(void)drawBody:(CGRect)rect withContext:(CGContextRef) ctx{
//画上半圆弧
CGFloat topX=kTopX;
CGFloat topY = kTopY;
CGFloat radius = kTopRadius;
CGContextAddArc(ctx, topX, topY, radius, -M_PI, 0, 0);
//画下半圆弧
CGFloat middX = topX + radius;
CGFloat middY = topY + 120;
CGContextAddLineToPoint(ctx, middX, middY);
CGFloat bottomRadius = radius;
CGFloat bottomX = topX;
CGFloat bottomY = middY;
CGContextAddArc(ctx, bottomX, bottomY, bottomRadius, 0, M_PI, 0);
CGContextClosePath(ctx);
[[UIColor colorWithRed:252/255.0 green:215/255.0 blue:0 alpha:1.0] set];
CGContextFillPath(ctx);
}
@end
源码在我的主页下面
标签:set,ctx,IOS,UIColor,黄人,绘图,kTopY,kTopX,CGFloat 来源: https://blog.csdn.net/A1521315qwss/article/details/117828196