#import "Birongchao.h"
@implementation Birongchao
- (void)drawRect:(CGRect)rect {
NSLog(@"drawRect 被调用了!");
[self drawLine];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawRectWithContext:context];
[self drawEllipse :context ];
[self drawArc:context];
[self drawCurve:context];
[self drawOneCurve:context];
}
-(void)drawLine{
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//拼接图形(路径)
//画一条直线
//设置起始位置
CGContextMoveToPoint(ctx, 0, 0);
//添加一条线段到(100,100)
CGContextAddLineToPoint(ctx, 375/2.0, 667/2.0);
CGContextAddLineToPoint(ctx, 375, 0);
CGContextClosePath(ctx);
//设置直线的宽度(黄色)
//CGContextSetRGBFillColor(ctx, 0, 1, 0, 1);
//set: 同时设置为空心和实心颜色
//setstroke:设置空心颜色
//setfill:设置实心颜色
[[UIColor redColor]set];
//设置直线的宽度
CGContextSetLineWidth(ctx, 10);
//设置填充颜色
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
//渲染颜色到view
// CGContextFillPath(ctx);
CGContextDrawPath(ctx,kCGPathFillStroke);
//设置直线开头的样式(圆)
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置直线转折点的样式(圆)
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//渲染显示到view上面
CGContextStrokePath(ctx);
//
// CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextMoveToPoint(context,0 , 0);
// CGContextAddLineToPoint(context, 375/2.0, 667/2.0);
// CGContextAddLineToPoint(context, 0,675);
// CGContextClosePath(context);
// [[UIColor greenColor]set];
// CGContextSetLineWidth(context, 10);
//
// CGContextSetRGBFillColor(context, 0, 1, 1, 1);
// CGContextDrawPath(context, kCGPathFillStroke);
// CGContextStrokePath(context);
}
#pragma mark 绘制矩形
-(void)drawRectWithContext:(CGContextRef)context{
// 肯定矩形图像的大小位置
CGRect rect = CGRectMake(20, 40, 335, 150);
CGContextAddRect(context, rect);
CGContextSetLineWidth(context, 20);
CGContextSetRGBFillColor(context, 1, 0.9, 0.5, 1);
// 设置矩形的属性
[[UIColor blueColor]setStroke];
// 绘制
CGContextDrawPath(context, kCGPathFillStroke);
// CGContextFillPath(context);
}
#pragma mark 绘制椭圆
-(void)drawEllipse :(CGContextRef)context{
CGRect rect = CGRectMake(50, 675/2, 275,275);
CGContextAddEllipseInRect(context, rect);
CGContextSetLineWidth(context, 20);
//设置属性
[[UIColor purpleColor]setFill];
//绘制
// CGContextDrawPath(context, kCGPathFillStroke);
CGContextFillPath(context);
}
#pragma mark 绘制弧
-(void) drawArc:(CGContextRef )context{
/*
x: 中心点x坐标
y: 中心点y坐标
radius:半径
startAngle :起始弧度
endAngle:结束弧度
closewise:是否逆时针绘制,0则顺时针绘制
*/
CGContextSetLineWidth(context, 20);
CGContextAddArc(context, 375/2.0, 667/2.0, 100, 0.0, M_PI_2, 1);
CGContextSetRGBFillColor(context, 1,0.5, 0.9, 1);
// 设置属性
[[UIColor orangeColor]set];
CGContextDrawPath(context, kCGPathFillStroke);
// CGContextFillPath(context);
}
#pragma mark 绘制贝塞尔曲线
-(void)drawCurve:(CGContextRef)context{
CGContextMoveToPoint(context, 20, 500);
/*
cp1x: 第一个控制点x坐标
cp1y: 第一个控制点y坐标
cp2x: 第二个控制点x坐标
cp2y: 第二个控制点x坐标
x: 结束点x坐标
y: 结束点y坐标
*/
CGContextAddCurveToPoint(context, 20, 300, 295, 500, 355, 300);
[[UIColor yellowColor]setFill];
[[UIColor redColor]setStroke];
CGContextMoveToPoint(context, 20, 500);
CGContextAddLineToPoint(context, 20, 300);
// CGContextAddLineToPoint(context, 295, 500);
CGContextSetLineWidth(context, 5);
CGContextMoveToPoint(context, 355, 300);
CGContextAddLineToPoint(context, 295, 500);
CGContextSetLineWidth(context, 5);
// 绘制
CGContextDrawPath(context, kCGPathFillStroke);
}
//一个控制点的贝塞尔曲线
-(void)drawOneCurve:(CGContextRef)context{
CGContextMoveToPoint(context, 20, 120);
CGContextAddLineToPoint(context, 187.5, 0);
CGContextMoveToPoint(context, 355, 120);
CGContextAddLineToPoint(context,187.5 , 0);
CGContextSetRGBStrokeColor(context, 0, 1, 1, 1);
CGContextMoveToPoint(context, 20, 120);
CGContextAddQuadCurveToPoint(context, 187.5, 0, 355, 120);
[[UIColor yellowColor]setFill];
[[UIColor redColor]setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
Birongchao *Bill = [[Birongchao alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
Bill.backgroundColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.9];
[self.view addSubview:Bill];
// Do any additional setup after loading the view, typically from a nib.
}.net
#import "myView.h"
@implementation myView
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawHat:context];
[self drawFace:context];
[self drawMouse:context];
[self drawTitle:context];
}
-(void)drawHat:(CGContextRef)context{
// CGContextMoveToPoint(context, 20, 120);
// CGContextAddLineToPoint(context, 187.5, 20);
// CGContextAddLineToPoint(context, 355, 120);
//
// CGContextSetRGBFillColor(context, 1,0, 0.5, 1);
// CGContextFillPath(context);
CGContextMoveToPoint(context, 20, 120);
CGContextAddQuadCurveToPoint(context, 187.5, 20, 355, 120);
[[UIColor purpleColor]setFill];
[[UIColor redColor]setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
}
-(void)drawFace:(CGContextRef)context{
// CGRect rect = CGRectMake(55, 100, 0, 350);
// CGContextAddEllipseInRect(context, rect);
// CGContextSetRGBFillColor(context,0.8,0.9, 0.8, 0.6);
// CGContextDrawPath(context,kCGPathFill);
CGContextMoveToPoint(context, 40, 120);
CGContextAddQuadCurveToPoint(context, 182, 520, 333, 120);
[[UIColor whiteColor]set];
CGContextDrawPath(context, kCGPathFillStroke);
}
-(void)drawMouse:(CGContextRef)context{
//眼珠
CGRect rect = CGRectMake(80, 155, 60, 40);
CGContextAddEllipseInRect(context, rect);
CGRect otherRect = CGRectMake(240, 155, 60, 40);
CGContextAddEllipseInRect(context, otherRect);
CGContextSetLineWidth(context, 3);
[[UIColor whiteColor]setFill];
[[UIColor blackColor]setStroke];
CGContextDrawPath(context, kCGPathFillStroke);
//眼仁
CGRect black = CGRectMake(95, 160, 30, 30);
CGContextAddEllipseInRect(context, black);
CGRect otherBlack = CGRectMake(255, 160, 30, 30);
CGContextAddEllipseInRect(context,otherBlack);
[[UIColor blackColor]set];
CGContextFillPath(context);
//嘴巴
CGContextMoveToPoint(context, 140, 270);
CGContextAddQuadCurveToPoint(context, 170, 330, 224, 270);
[[UIColor colorWithRed:0.5 green:0.5 blue:0.8 alpha:0.9]set];
CGContextDrawPath(context, kCGPathFill);
//耳朵
CGContextMoveToPoint(context, 320, 160);
CGContextAddQuadCurveToPoint(context, 370, 190,285, 233);
CGContextMoveToPoint(context, 53, 160);
CGContextAddQuadCurveToPoint(context, 13, 190, 85, 233);
CGContextDrawPath(context, kCGPathFill);
//鼻子
CGContextMoveToPoint(context, 182, 200);
CGContextAddLineToPoint(context, 172, 255);
CGContextAddLineToPoint(context, 192, 255);
CGContextSetRGBFillColor(context, 0.7, 0.3, 0.4, 1);
CGContextFillPath(context);
}
-(void)drawTitle:(CGContextRef )context{
CGContextMoveToPoint(context, 20, 550);
CGContextAddCurveToPoint(context, 20,350, 295, 550, 355, 350);
CGContextMoveToPoint(context, 20, 350);
CGContextAddCurveToPoint(context, 20, 550, 295, 350, 355, 550);
[[UIColor redColor]setFill];
[[UIColor greenColor]setStroke];
CGContextMoveToPoint(context, 20, 350);
CGContextAddCurveToPoint(context, 220, 350, 155, 550, 355, 550);
CGContextMoveToPoint(context, 20, 550);
CGContextAddCurveToPoint(context, 220, 550, 155, 350, 355, 350);
CGContextDrawPath(context, kCGPathFillStroke);
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
myView *view = [[myView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
view.backgroundColor = [UIColor colorWithRed:0.8 green:0.6 blue:0.8 alpha:0.9];
[self.view addSubview:view];
}
ip