用Quartz 2D画小黄人

时间:2023-12-06 11:09:56

第一步:

先创建一个OneView类,并在storyboard里边拖拽一个UIview,将这个UIview的类改成OneView。如图所示:

用Quartz 2D画小黄人

第二步:

在新创建的Oneview里,补齐下列代码:

 //
// OneView.m
// 03-drawImage
//
// Created by jerry on 15/7/20.
// Copyright (c) 2015年 jerry. All rights reserved.
// #import "OneView.h" @implementation OneView -(void)drawRect:(CGRect)rect
{
// [self drawImage];
[self drawYellowPop:rect];
}
//
-(void)drawYellowPop:(CGRect)rect
{
// 先获取图片上下文对象
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置头及颜色
[self setHeaderWithCGContextRef:ctx andRect:rect]; [self setMouthWith:ctx andRect:rect]; [self setGlassesWith:ctx andRect:rect]; [self setHairWith:ctx andRect:rect]; }
/**
* 画嘴
*
* @param ctx <#ctx description#>
* @param rect <#rect description#>
*/
-(void)setMouthWith:(CGContextRef)ctx andRect:(CGRect)rect
{
// 设置中间点
CGFloat CenterX = rect.size.width * 0.5;
CGFloat CenterY = rect.size.width * 0.5; // 设置当前点
CGFloat marginX = ;
CGFloat marginY = ;
CGFloat currenX = CenterX - marginX;
CGFloat currenY = CenterY - marginY;
CGContextMoveToPoint(ctx, currenX, currenY); // 结束点
CGFloat endX = CenterX + marginX;
CGFloat endY = currenY;
// 贝塞尔曲线
CGContextAddQuadCurveToPoint(ctx, CenterX, CenterY, endX, endY); // 设置颜色
[[UIColor blackColor]set];
// 渲染
CGContextStrokePath(ctx);
}
// 设置眼镜
-(void)setGlassesWith:(CGContextRef)ctx andRect:(CGRect)rect
{
CGFloat LineX = rect.size.width * 0.5 - ; // 减半径
CGFloat LineY = ;
// 画一条黑线
CGContextMoveToPoint(ctx,LineX , LineY);
// 结束点
CGFloat EndLineX = LineX + * ;
CGFloat EndLineY = LineY;
CGContextAddLineToPoint(ctx,EndLineX, EndLineY);
CGContextSetLineWidth(ctx, );
[[UIColor blackColor]set];
CGContextStrokePath(ctx); // 2.画眼
// 注释:画眼就是画圆
CGFloat leftEyesY = LineY;
CGFloat leftEyesRadius = ;
CGFloat leftEyesX = rect.size.width *0.5 - leftEyesRadius;
CGFloat leftEyesStartAngle = ;
CGFloat leftEyesEndAngle = M_PI * ;
[[UIColor redColor]set];
CGContextAddArc(ctx, leftEyesX, leftEyesY, leftEyesRadius, leftEyesStartAngle, leftEyesEndAngle, );
CGContextFillPath(ctx); CGFloat rightEyesX = leftEyesX + leftEyesRadius * ;
CGFloat rightEyesY = leftEyesY;
CGFloat rightEyesSstartAngle = ;
CGFloat rightEyesRadius = leftEyesRadius;
CGFloat rightEyesEndAngle = leftEyesEndAngle;
CGContextAddArc(ctx, rightEyesX, rightEyesY, rightEyesRadius, rightEyesSstartAngle, rightEyesEndAngle, );
[[UIColor redColor]set];
CGContextFillPath(ctx); // 镜片
CGFloat leftGlassesRadius = ;
CGFloat leftGlassesX = leftEyesX;
CGFloat leftGlassesY = LineY;
CGContextAddArc(ctx, leftGlassesX, leftGlassesY, leftGlassesRadius, , M_PI * , ); CGFloat rightGlassesRadius = leftGlassesRadius;
CGFloat rightGlassesX = rightEyesX;
CGFloat rightGlassesY = LineY;
CGContextAddArc(ctx, rightGlassesX, rightGlassesY, rightGlassesRadius,, M_PI * , );
[[UIColor whiteColor]set];
CGContextFillPath(ctx); // 眼睛珠
CGFloat leftZhuRadius = ;
CGFloat leftZhuY = LineY;
CGFloat leftZhuX = leftGlassesX +;
CGContextAddArc(ctx, leftZhuX, leftZhuY, leftZhuRadius, , M_PI * , ); CGFloat rightZhuRadius = leftZhuRadius;
CGFloat rightZhuY = LineY;
CGFloat rightZhuX = rightGlassesX - ;
CGContextAddArc(ctx, rightZhuX, rightZhuY, rightZhuRadius, , M_PI * , ); [[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:]set];
CGContextFillPath(ctx); // 左瞳孔
CGFloat leftTongRadius = ;
CGFloat leftTongY = leftZhuY;
CGFloat leftTontX = leftZhuX;
CGContextAddArc(ctx, leftTontX, leftTongY, leftTongRadius, , M_PI * , ); // 右瞳孔
CGFloat rightTongRadius = leftTongRadius;
CGFloat rightTongX = rightZhuX;
CGFloat rightTongY = rightZhuY;
CGContextAddArc(ctx, rightTongX, rightTongY, rightTongRadius, , M_PI *, );
[[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:]set];
CGContextFillPath(ctx); // 左聚光
CGFloat leftJuRadius = ;
CGFloat leftJuX = leftTontX - ;
CGFloat leftJuY =leftTongY - ;
CGContextAddArc(ctx, leftJuX, leftJuY, leftJuRadius, , M_PI * , ); // 右聚光
CGFloat rightJuRadius = leftJuRadius;
CGFloat rightJuX = rightTongX - ;
CGFloat rightJuY = rightTongY - ;
CGContextAddArc(ctx, rightJuX, rightJuY, rightJuRadius, , M_PI *, );
[[UIColor whiteColor]set];
CGContextFillPath(ctx);
} -(void)setHeaderWithCGContextRef:(CGContextRef)ctx andRect:(CGRect)rect
{
// 画图片
CGFloat topX = rect.size.width * 0.5; // 确定x 轴的点
CGFloat topY = ;
CGFloat topRadius = ; // 半径
CGFloat topStartAngle = ; // 起点
CGFloat topEndAngle = M_PI;
// 0 是顺时针 1 是逆时针
CGContextAddArc(ctx, topX, topY, topRadius, topStartAngle, topEndAngle, );
// 以上一个终点为起点画直线
CGFloat leftLineX = topX - topRadius;
CGFloat leftLineY = topY + ;
CGContextAddLineToPoint(ctx, leftLineX, leftLineY); CGFloat downX = topX;
CGFloat downY = leftLineY;
CGContextAddArc(ctx, downX, downY, topRadius,topEndAngle , topStartAngle, );
// 关闭路径
CGContextClosePath(ctx);
// 填充背景颜色
[[UIColor yellowColor]set];
//显示在view
CGContextFillPath(ctx);
}
/**
* 设置头发
*
*/
- (void)setHairWith:(CGContextRef)ctx andRect:(CGRect)rect
{
// 第一根头发
CGFloat hairStartX = rect.size.width *0.5;
CGFloat hairStartY = ;
CGContextMoveToPoint(ctx, hairStartX, hairStartY);
CGFloat hairEndX = rect.size.width * 0.5 + ;
CGFloat hairEndY = ;
CGContextAddLineToPoint(ctx, hairEndX, hairEndY); // 第二根头发
CGFloat hairTwoStartX = rect.size.width * 0.5 + ;
CGFloat hairTwoStartY = ;
CGContextMoveToPoint(ctx, hairTwoStartX, hairTwoStartY);
CGFloat hairTowEndX = rect.size.width * 0.5 + ;
CGFloat hairTowEndY = ;
CGContextAddLineToPoint(ctx, hairTowEndX, hairTowEndY); // 第三根头发
CGFloat hairThreeStartX = rect.size.width * 0.5 - ;
CGFloat hairThreeStartY = ;
CGContextMoveToPoint(ctx, hairThreeStartX, hairThreeStartY);
CGFloat hairThreeEndX = rect.size.width * 0.5 - ;
CGFloat hairThreeEndY = ;
CGContextAddLineToPoint(ctx, hairThreeEndX, hairThreeEndY); // 第四个头发
CGFloat hairFourStartX = rect.size.width * 0.5 - ;
CGFloat hairFourStartY = ;
CGContextMoveToPoint(ctx, hairFourStartX, hairFourStartY);
CGFloat hairFourEndX = rect.size.width *0.5 - ;
CGFloat hairFourEndY = ;
CGContextAddLineToPoint(ctx, hairFourEndX, hairFourEndY); // 第五根头发
CGFloat hairFiveStartX = rect.size.width * 0.5 + ;
CGFloat hairFiveStartY = ;
CGContextMoveToPoint(ctx, hairFiveStartX, hairFiveStartY);
CGFloat hairFiveEndX = rect.size.width * 0.5 + ;
CGFloat hairFiveEndY = ;
CGContextAddLineToPoint(ctx, hairFiveEndX, hairFiveEndY); [[UIColor blackColor]set];
CGContextSetLineWidth(ctx, );
CGContextStrokePath(ctx); }
/**
* 画图片
*/
-(void)drawImage
{
// 用oc代码 可以不用获取图形上下文对象
UIImage *img = [UIImage imageNamed:@""];
[img drawAsPatternInRect:CGRectMake(, , , )]; }
@end

最后运行显示的结果如下:

用Quartz 2D画小黄人