//
//PathView.m
//CocoTest_1
//
//Created by S u p e r m a n on 2017/3/14.
//Copyright © 2017年张浩. All rights reserved.
//
#import"PathView.h"
@implementationPathView
- (instancetype)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
self.backgroundColor = [UIColor blueColor];
}
returnself;
}
- (void)drawRect:(CGRect)rect {
[selfCGPath];
[selfCGContext];
[selfbezier];
[selfdrawRound];
}
/*
*一:CGPath
**/
- (void)CGPath {
CGContextRef ref = UIGraphicsGetCurrentContext();
CGMutablePathRef pathRef = CGPathCreateMutable();
//1.设置起点
CGPathMoveToPoint(pathRef,nil,5,5);
//2.加入线
CGPathAddLineToPoint(pathRef,nil,50,50);
//3.把path放到contextRef中
CGContextAddPath(ref, pathRef);
CGPathRelease(pathRef);
[[UIColor redColor]setStroke];
//CGContextSetStrokeColorWithColor(ref, [UIColor redColor].CGColor);
/*
*这句话必须加上去否则不会开始绘制
*这句话是开始绘制CGPath路径
**/
CGContextStrokePath(ref);
}
/*
*二:直接用CGContext绘制
**/
- (void)CGContext {
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ref,50,50);
CGContextAddLineToPoint(ref,100,100);
CGContextAddLineToPoint(ref,10,60);
CGContextSetLineWidth(ref,5);
CGContextSetLineCap(ref, kCGLineCapRound);
CGContextSetLineJoin(ref, kCGLineJoinBevel);
[[UIColor yellowColor]setFill];
[[UIColor cyanColor]setStroke];
/*
* 3种方法绘制path
* 1.CGContextFillPath(ref);绘制填充
* 2.CGContextStrokePath(ref);绘制笔触
* 3.CGContextDrawPath(ref, kCGPathFillStroke);绘制填充和笔触
**/
CGContextDrawPath(ref, kCGPathFillStroke);
}
/*
*贝塞尔曲线
**/
- (void)bezier {
UIBezierPath * path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100,100)];
[path addLineToPoint:CGPointMake(200,100)];
[path addLineToPoint:CGPointMake(150,200)];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinBevel];
[path setLineWidth:5];
[[UIColor redColor]setFill];
[[UIColor yellowColor]setStroke];
[path stroke];
[path fill];
[path closePath];
}
/*
*贝塞尔曲线绘制圆,椭圆,扇形
**/
- (void)drawRound {
//绘制圆
UIBezierPath * path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(100,100) radius:50startAngle:0endAngle:M_PI*1.5clockwise:YES];
[path setLineWidth:10];
[[UIColor yellowColor]setStroke];
[path stroke];
//绘制椭圆
UIBezierPath * path1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,200)];
[path1 setLineWidth:5];
[path1 stroke];
}
@end