上一篇文章介紹了CoreGraphics,UIBezierPath就是路徑繪制的UIKit封裝,可以繪制矩形曲線等簡(jiǎn)單的圖形。所以,同樣的,使用UIBezierPath繪制圖形也是要在drawRect:方法中進(jìn)行。
大體步驟為:
- 重寫view 的
drawRect:方法- 創(chuàng)建
UIBezierPath對(duì)象moveToPoint:設(shè)置路徑起始點(diǎn)- 使用
UIBezierPath的類方法繪制需要的路徑- 設(shè)置圖形屬性
- 繪制圖形
使用UIBezierPath繪制簡(jiǎn)單的圖形
//1.創(chuàng)建UIBezierPath
UIBezierPath *path = [UIBezierPath bezierPath];
//2.繪制路徑
[path moveToPoint:CGPointMake(50, 300)];
[path addLineToPoint:CGPointMake(50, 200)];
[path addCurveToPoint:CGPointMake(350, 200) controlPoint1:CGPointMake(160, 50) controlPoint2:CGPointMake(240, 350)];
[path addLineToPoint:CGPointMake(350, 300)];
[path closePath];
//3.設(shè)置圖形屬性
[[UIColor redColor] setStroke];
[[UIColor blueColor] setFill];
path.lineWidth = 5;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
//4.繪制圖形
[path fill];

UIBezierPathView1.png
一些常用屬性
//路徑寬度
@property(nonatomic) CGFloat lineWidth;
//路徑端點(diǎn)樣式
/*
kCGLineCapButt 默認(rèn),平的
kCGLineCapRound 圓的
kCGLineCapSquare 矩形
*/
@property(nonatomic) CGLineCap lineCapStyle;
//路徑拐點(diǎn)樣式
/*
kCGLineJoinMiter 拐點(diǎn)形狀為尖的
kCGLineJoinRound 拐點(diǎn)形狀為圓的
kCGLineJoinBevel 拐點(diǎn)形狀為平的
*/
@property(nonatomic) CGLineJoin lineJoinStyle;
//最大斜接長(zhǎng)度 斜接長(zhǎng)度指的是在兩條線交匯處內(nèi)角和外角之間的距離,如果正常的斜接長(zhǎng)度大于miterLimit的值,則顯示類型類似kCGLineJoinBevel
@property(nonatomic) CGFloat miterLimit; // Used when lineJoinStyle is kCGLineJoinMiter
//確定路徑渲染的精確度,默認(rèn)值為0.6,值越小,渲染精度越高,計(jì)算時(shí)間也越長(zhǎng)。
@property(nonatomic) CGFloat flatness;
//渲染規(guī)則,設(shè)置為yes則路徑會(huì)使用奇偶規(guī)則填充,設(shè)置為no使用非零規(guī)則填充。
@property(nonatomic) BOOL usesEvenOddFillRule;
//獲取路徑的終點(diǎn),如果當(dāng)前路徑為空, 那么該屬性的值將會(huì)是 CGPointZero
@property(nonatomic, readonly) CGPoint currentPoint;
一些常用方法
//創(chuàng)建一個(gè)路徑對(duì)象
+ (instancetype)bezierPath;
//創(chuàng)建一個(gè)矩形
+ (instancetype)bezierPathWithRect:(CGRect)rect;
//創(chuàng)建一個(gè)橢圓,內(nèi)切于矩形
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
//創(chuàng)建一個(gè)圓角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
//圓角位置可選的矩形
//rect:矩形位置及尺寸
//corners:圓弧所在角的位置,可多選
/*
UIRectCornerTopLeft 左上角
UIRectCornerTopRight 右上角
UIRectCornerBottomLeft 左下角
UIRectCornerBottomRight 右下角
UIRectCornerAllCorners 所有角
*/
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
//創(chuàng)建一個(gè)弧形
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
//利用CGPathRef創(chuàng)建路徑
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
//路徑移動(dòng)到某點(diǎn)
- (void)moveToPoint:(CGPoint)point;
//從當(dāng)前點(diǎn)劃一條到指定點(diǎn)的直線
- (void)addLineToPoint:(CGPoint)point;
//從當(dāng)前點(diǎn)劃一條到指定點(diǎn)的三次貝塞爾曲線
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
//從當(dāng)前點(diǎn)劃一條到指定點(diǎn)的二次貝塞爾曲線
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
//添加一段弧線
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise NS_AVAILABLE_IOS(4_0);
//關(guān)閉路徑
- (void)closePath;
//刪除路徑內(nèi)的所有點(diǎn)
- (void)removeAllPoints;
//拼接已經(jīng)存在的路徑
- (void)appendPath:(UIBezierPath *)bezierPath;
//返回一個(gè)當(dāng)前路徑翻轉(zhuǎn)后的路徑
- (UIBezierPath *)bezierPathByReversingPath NS_AVAILABLE_IOS(6_0);
//對(duì)路徑中的所有點(diǎn)進(jìn)行仿射變換
- (void)applyTransform:(CGAffineTransform)transform;
//設(shè)置虛線
//pattern c語言數(shù)組,表示的是虛線的實(shí)線長(zhǎng)度和實(shí)現(xiàn)中間間隔距離
//count pattern數(shù)組的個(gè)數(shù)
//phase 虛線從哪個(gè)位置開始繪制
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
//重新獲取之前設(shè)置過的虛線樣式
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase;
//路徑中是否包含某個(gè)點(diǎn),如果是封閉路徑,則返回封閉區(qū)域是否包含某個(gè)點(diǎn)
- (BOOL)containsPoint:(CGPoint)point;
//繪制路徑中間的填充
- (void)fill;
//繪制路徑
- (void)stroke;
//以指定模式填充路徑封閉的范圍
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//以指定模式繪制路徑
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
//剪切區(qū)域,路徑之外的區(qū)域?qū)⒉槐讳秩?- (void)addClip;
繪制矩形
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 100)];
[[UIColor blueColor] set];
[rectPath fill];

UIBezierPathView2.png
繪制橢圓
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)];
[[UIColor blueColor] set];
ovalPath.lineWidth = 2;
[ovalPath stroke];

UIBezierPathView3.png
圓角矩形
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 200, 100) cornerRadius:10];
[[UIColor blueColor] set];
rectPath.lineWidth = 2;
[rectPath stroke];

UIBezierPathView4.png
自定義圓角矩形
UIBezierPath *rectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 200, 100) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
[[UIColor blueColor] set];
rectPath.lineWidth = 2;
[rectPath stroke];

UIBezierPathView5.png
弧形
UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:-M_PI_2 endAngle:M_PI_4 clockwise:YES];
[[UIColor blueColor] set];
[arcPath fill];

UIBezierPathView6.png