//ide
// ViewController.mui
// Quartz2Dspa
//.net
// Created by dc008 on 15/12/7.3d
// Copyright © 2015年 CXY. All rights reserved.code
//orm
#import "ViewController.h"ip
#import "CuiXiaoYu.h"get
@interface ViewController ()animation
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CuiXiaoYu *cxy = [[CuiXiaoYu alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
// cxy.backgroundColor =[UIColor redColor];
[self.view addSubview:cxy];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//
// CuiXiaoYu.m
// Quartz2D
//
// Created by dc008 on 15/12/7.
// Copyright © 2015年 CXY. All rights reserved.
//
#import "CuiXiaoYu.h"
@implementation CuiXiaoYu
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)drawRect:(CGRect)rect{
NSLog(@"drawRect被調用了");
// [self drawLine];
CGContextRef ctx = UIGraphicsGetCurrentContext();
// [self drawRectWithContext:ctx];
// [self drawEllipse:ctx];
// [self drawArc:ctx];
// [self drawCurve:ctx];
[self drawOneCurve:ctx];
}
#pragma mark 繪製線
- (void)drawLine{
//1.得到圖形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRef context = UIGraphicsGetCurrentContext();
//2.拼接圖形(路徑)
//畫一條直線
//設置起始位置
CGContextMoveToPoint(ctx, 0, 0);
CGContextMoveToPoint(context, 10, 10);
//添加一條線段到點(100,100)
CGContextAddLineToPoint(ctx, 375/2.0, 667/2.0);
CGContextAddLineToPoint(ctx, 375, 0);
CGContextAddLineToPoint(context, 375, 667);
//設置直線的顏色(黃色)
// CGContextSetRGBStrokeColor (ctx, 0, 0 , 1 , 1 );
//set : 同時設置爲空心和實心顏色
//stroke :設置空心顏色
//fill : 設置實心顏色
[[UIColor redColor]set];
//設置填充顏色
CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);
CGContextSetRGBFillColor(context, 0, 0, 2, 2);
// [[UIColor yellowColor] set];
//渲染顏色到view
// CGContextFillPath(ctx);// 實心渲染
// CGContextStrokePath(ctx);// 空心渲染
//閉合路徑 自動添加線來閉合圖形
CGContextClosePath(ctx);
//設置直線的寬度
CGContextSetLineWidth(ctx, 10);
CGContextSetLineWidth(context, 10);
//設置直線開頭的樣式(圓)
CGContextSetLineCap(ctx, kCGLineCapRound);
//設置直線轉折點的樣式(圓)
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//3.渲染顯示到View上面
// CGContextFillPath(ctx); 實心渲染
// CGContextStrokePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);
CGContextDrawPath(context, kCGPathEOFillStroke);
}
#pragma mark 繪製矩形
- (void) drawRectWithContext : (CGContextRef)context{
//肯定矩形圖像的大小位置
CGRect rect = CGRectMake(20, 20, 335, 50);
CGContextAddRect(context, rect);
//設置矩形的屬性
[[UIColor whiteColor]set];
//繪製
CGContextDrawPath(context, kCGPathFillStroke);
}
#pragma mark 繪製橢圓
- (void) drawEllipse : (CGContextRef)context {
CGRect rect = CGRectMake(100, 100, 200, 200);
CGContextAddEllipseInRect(context, rect);
//設置屬性
[[UIColor grayColor]set];
//繪製
CGContextDrawPath(context, kCGPathFillStroke);
}
#pragma mark 繪製弧
- (void) drawArc : (CGContextRef)context {
/*
x:中心點x座標
y:中心點y座標
radius:半徑
startAngle:起始弧度
endAngle:結束弧度
clockwise:是否逆時針繪製,0則順時針繪製
*/
CGContextSetLineWidth(context, 20);
CGContextAddArc(context, 375/2.0, 667/2.0,100 , 0.0, M_PI_2, 1);
//設置屬性
[[UIColor orangeColor]setStroke];
//繪製
CGContextDrawPath(context, kCGPathStroke);
}
#pragma mark 繪製貝塞爾曲線
- (void) drawCurve : (CGContextRef) context {
CGContextMoveToPoint(context, 20, 500);
/*
cp1x:第一個控制點x座標
cp1y:第一個控制點y座標
cp2x:第二個控制點x座標
cp2y:第二個控制點y座標
x:結束點x座標
y:結束點y座標
*/
CGContextAddCurveToPoint(context, 20, 300, 295, 500, 355, 300);
//設置曲線屬性
[[UIColor yellowColor]setFill];
[[UIColor redColor]setStroke];
//繪製
CGContextDrawPath(context, kCGPathFillStroke);
//設置第一條線
CGContextMoveToPoint(context,20 , 500);
CGContextAddLineToPoint(context, 20, 300);
CGContextSetLineWidth(context, 5);
//設置第二條線
CGContextMoveToPoint(context, 355, 300);
CGContextAddLineToPoint(context, 295, 500);
CGContextSetLineWidth(context, 5);//設置邊框寬度
CGContextDrawPath(context, kCGPathFillStroke);
}
#pragma mark 繪製一個控制點的貝塞爾曲線
-(void) drawOneCurve : (CGContextRef) context {
//移動到曲線起始位置
CGContextMoveToPoint(context, 20, 100);
CGContextAddQuadCurveToPoint(context, 187.5, 0, 355, 100);
[[UIColor redColor]setFill];
[[UIColor whiteColor]setStroke];
//繪製
CGContextDrawPath(context, kCGPathFillStroke);
}
@end