#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