#import "DrawView.h"
#import <CoreGraphics/CoreGraphics.h>
@implementation DrawView
- (void)drawRect:(CGRect)rect {
// drawRect: 此方法不能手动调用,可以调用以下方法间接调用
// [self setNeedsDisplay];
CGContextRef context = [self createCobtext];
// [self drawLineOne:context];
//
// [self drawLineTwo:context];
//
// [self drawCylye:context];
[self drawBezierPath:context];
}
//初始化画布
- (CGContextRef )createCobtext {
// CGContextRef是一个不透明的绘画环境, 简单理解为一块画布
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置颜色
CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);
// 设置画笔的宽度
CGContextSetLineWidth(context, 5);
return context;
}
//第一种画线方式
- (void)drawLineOne:(CGContextRef)context {
// 设置起点
CGContextMoveToPoint(context, 20, 20);
// 设置下一个点
CGContextAddLineToPoint(context, 200, 200);
CGContextAddLineToPoint(context, 200, 500);
// 绘制
CGContextStrokePath(context);
}
//第二种方式
- (void)drawLineTwo:(CGContextRef )context {
// 通过路径来画线
CGMutablePathRef path = CGPathCreateMutable();
// 设置起点
// CGAffineTransformIdentity表示见当前的view的transform设为初始值
CGPathMoveToPoint(path, &CGAffineTransformIdentity, 20, 20);
// 添加另一个点
CGPathAddLineToPoint(path, &CGAffineTransformIdentity, 100, 300);
// 给画布添加路径,再次路径上画线
CGContextAddPath(context, path);
// 绘制
CGContextStrokePath(context);
}
//画圆弧
- (void)drawCylye:(CGContextRef )context {
// 参数!:context
// 参数2:圆心x
// 参数3:圆心y
// 参数4:半径
// 参数5:起始角度
// 参数6:终点角度
// 参数7:0表示顺时针,1表示逆时针
CGContextAddArc(context, 100, 100, 50, 0, M_PI*2, 0);
// 绘制
CGContextStrokePath(context);
}
//贝塞尔曲线
- (void)drawBezierPath:(CGContextRef)context {
CGContextMoveToPoint(context, 50, 50);
// 设置下一点
// 参数1:context
// 参数2:端点1x
// 参数3:端点1y
// 参数4:端点2x
// 参数5:端点3y
// 参数6:半径
CGContextAddArcToPoint(context, 100, 300, 300, 100, 100);
CGContextStrokePath(context);
}
@end