1、案例一:画小黄人
/**
掌握重点:
1、掌握宏的定义和使用,这个类似JAVA中方法
2、灵活在C语言和C#中切换
3、了解贝塞尔曲线,得知道它有一个起始点
4、
*/
//定义全局引用的宏
#define FLColor(r,g,b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]
#define FLRdius 70
#define FLTopY 100
#import "HumanView.h"
@implementation HumanView
- (void)drawRect:(CGRect)rect
{
//1、上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//2、身体
drawBody(ctx, rect);
//3、微笑
drawMouse(ctx, rect);
//4、画眼睛
drawEyes(ctx, rect);
}
/**
* 画眼睛
*/
void drawEyes(CGContextRef ctx, CGRect rect)
{
//1、绑带
CGFloat startX = rect.size.width * 0.5 - FLRdius;
CGFloat startY = FLTopY;
CGContextMoveToPoint(ctx, startX, startY);
CGFloat endX = startX + 2 * FLRdius;
CGFloat endY = startY;
CGContextAddLineToPoint(ctx, endX, endY);
//线宽
CGContextSetLineWidth(ctx, 15);
//颜色
[[UIColor blackColor] set];
//渲染
CGContextStrokePath(ctx);
//2、最外层镜框
[FLColor(61, 62, 66) set];
CGFloat kuangRadius = FLRdius * 0.4;
CGFloat kuangY = startY;
CGFloat kuangX = rect.size.width * 0.5 - kuangRadius;
CGContextAddArc(ctx, kuangX, kuangY, kuangRadius, 0, 2 * M_PI, 0);
CGContextFillPath(ctx);
//3、里面白色框
[[UIColor whiteColor] set];
CGFloat whiteX = kuangX;
CGFloat whiteY = kuangY;
CGFloat whiteRadius = kuangRadius * 0.7;
CGContextAddArc(ctx, whiteX, whiteY, whiteRadius, 0, M_PI * 2, 0);
CGContextFillPath(ctx);
}
/**
* 画嘴巴
*/
void drawMouse(CGContextRef ctx, CGRect rect)
{
/**
* 这里采用的贝塞尔曲线:采用这种曲线画图,得知道当前点,然后再画出起始点和结束点
*/
//中间的控制点
CGFloat controlX = rect.size.width * 0.5;
CGFloat controlY = rect.size.height * 0.5 - 140;
//当前点
CGFloat marginX = 30;
CGFloat marginY = 30;
CGFloat currentX = controlX - marginX;
CGFloat currentY = controlY - marginY;
CGContextMoveToPoint(ctx, currentX, currentY);
//结束点
CGFloat endX = controlX + marginX;
CGFloat endY = currentY;
//贝塞尔曲线
CGContextAddQuadCurveToPoint(ctx, controlX, controlY, endX, endY);
//填色:
[[UIColor blackColor] set];
//渲染
CGContextStrokePath(ctx);
}
/**
* 画身体
*/
void drawBody(CGContextRef ctx, CGRect rect)
{
//上半圆
CGFloat topX = rect.size.width * 0.5;
CGFloat topY = FLTopY;
CGFloat topRadius = FLRdius;//半径
CGContextAddArc(ctx, topX, topY, topRadius, 0, M_PI, 1);
//向下延伸
CGFloat middleX = topX - topRadius;
CGFloat middleH = 100;//中间高度
CGFloat middleY = topY + middleH;
CGContextAddLineToPoint(ctx, middleX, middleY);
//下半圆
CGFloat bottomX = topX;
CGFloat bottomY = middleY;
CGFloat bottomRadius = topRadius;
CGContextAddArc(ctx, bottomX, bottomY, bottomRadius, M_PI, 0, 1);
//合并路径
CGContextClosePath(ctx);
//填色:利用填充方式画出之前的路径
[FLColor(252, 218, 0) set];
//显示
CGContextFillPath(ctx);
}
@end
画小黄人
最新推荐文章于 2022-06-27 18:37:06 发布