//
// DrawView.m
// 01-基本线条绘制(画线)(熟悉)
//
// Created by 李亮 on 2016/11/30.
// Copyright © 2016年 www.thelast.com. All rights reserved.
//
#import "DrawView.h"
@implementation DrawView
- (void)drawRect:(CGRect)rect {
[self drawCurve];
}
//曲线
- (void)drawCurve{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath * path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 200)];
CGPoint p1 = CGPointMake(175, 200);
CGPoint p2 = CGPointMake(300, 100);
[path addCurveToPoint:CGPointMake(300, 100) controlPoint1:p1 controlPoint2:p2];
[[UIColor yellowColor] set];
// [path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(175, 100)];
CGContextAddPath(ctx, path.CGPath);
CGContextStrokePath(ctx);
}
//链接线
- (void)drawlinkLines{
//获取上下方
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//生成路径
UIBezierPath * path = [UIBezierPath bezierPath];
CGPoint startP = CGPointMake(50, 200);
//第一条
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(200, 200)];
//链接第二条
[path addLineToPoint:CGPointMake(200, 100)];
//链接第3条
[path addLineToPoint:CGPointMake(50, 100)];
//链接第4条
[path addLineToPoint:CGPointMake(50, 200)];
//设置线宽
CGContextSetLineWidth(contextRef, 10);
//链接角
CGContextSetLineJoin(contextRef, kCGLineJoinRound);
//颜色
[[UIColor redColor] set];
//设置线的顶角样式
CGContextSetLineCap(contextRef, kCGLineCapRound);
CGContextAddPath(contextRef, path.CGPath);
//关闭上下文
// CGContextFillPath(contextRef);
CGContextStrokePath(contextRef);
}
//画多条线
- (void)drawLines{
//获取上下方
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//生成路径
UIBezierPath * path = [UIBezierPath bezierPath];
CGPoint startP = CGPointMake(50, 200);
//第一条
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(200, 200)];
//第二条
[path moveToPoint:CGPointMake(50, 100)];
[path addLineToPoint:CGPointMake(200, 100)];
CGContextAddPath(contextRef, path.CGPath);
//关闭上下文
CGContextStrokePath(contextRef);
}
//画一条线
- (void)drawLine{
//获取上下方
CGContextRef contextRef = UIGraphicsGetCurrentContext();
//生成路径
UIBezierPath * path = [UIBezierPath bezierPath];
CGPoint startP = CGPointMake(50, 200);
[path moveToPoint:startP];
[path addLineToPoint:CGPointMake(200, 200)];
CGContextAddPath(contextRef, path.CGPath);
//关闭上下文
CGContextStrokePath(contextRef);
}
@end
需要注意的是
CGPoint p1 = CGPointMake(175, 200);
CGPoint p2 = CGPointMake(300, 100);
[path addCurveToPoint:CGPointMake(300, 100) controlPoint1:p1 controlPoint2:p2];
[path addQuadCurveToPoint:CGPointMake(300, 200) controlPoint:CGPointMake(175, 100)];
这俩个方法,一个是设置一个点画曲线,另外一个是设置俩个点画曲线,有什么呢?
比如我上方给的俩个坐标点,如果p2给CGPointMake(300, 200);那么曲线会向下弯曲。如果给CGPointMake(300, 100),则会往上弯曲。如图