//
// 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),則會往上彎曲。如圖
WechatIMG3.jpeg
WechatIMG5.jpeg