title: iOS UIBezierPath 繪制圖形
date: 2016-07-18 18:29:12
categories:
- Code
- iOS
tags: - UIBezierPath
閑著沒事,研究一下ios中繪制圖形,這是第一課,先看 UIBezierPath使用詳解 各個屬性及方法。 附帶 實例代碼。
UIBezierPath類介紹
// 初始化方法
+ (instancetype)bezierPath;
// 創(chuàng)建矩形
+ (instancetype)bezierPathWithRect:(CGRect)rect;
// 創(chuàng)建圓形或者橢圓 這個方法根據(jù)傳入的rect矩形參數(shù)繪制一個內(nèi)切曲線。
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
// 創(chuàng)建圓角矩形(橢圓)
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
// 創(chuàng)建可以指定圓角位置的矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
/*
使用UIBezierPath創(chuàng)建一段弧線 其中的
參數(shù)分別指定:這段圓弧的中心,半徑,開始角度,結(jié)束角度,是否順時針方向
*/
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
// 通過已有路徑創(chuàng)建路徑:
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;
// init方法
- (instancetype)init NS_DESIGNATED_INITIALIZER;
// initWiteCoder方法:
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
// 將UIBezierPath類轉(zhuǎn)換成CGPath,類似于UIColor的CGColor
@property(nonatomic) CGPathRef CGPath;
- (CGPathRef)CGPath
// Path construction
// 初始點的位置坐標(biāo) x,y
- (void)moveToPoint:(CGPoint)point;
// 結(jié)束點的位置坐標(biāo)
- (void)addLineToPoint:(CGPoint)point;
// 繪制三次貝塞爾曲線
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
799670-20151013172626304-936217179.png
// 繪制二次貝塞爾曲線
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
799670-20151013173704351-1059522850.png
// 繪制弧形線段
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise
// 結(jié)束繪制 會自動鏈接結(jié)束點和初始點
- (void)closePath;
// 刪除所有路徑
- (void)removeAllPoints;
/* 這兩個方法不知道怎么用 有人會用的話 請告知
- (void)appendPath:(UIBezierPath *)bezierPath
- (void)applyTransform:(CGAffineTransform)transform
*/
// Appending paths
- (void)appendPath:(UIBezierPath *)bezierPath;
// Modified paths
// 扭轉(zhuǎn)路徑,即起點變成終點,終點變成起點
- (UIBezierPath *)bezierPathByReversingPath
// Transforming paths
// 路徑進(jìn)行仿射變換
- (void)applyTransform:(CGAffineTransform)transform;
// Path info
// 只讀類型,路徑上是否有有效的元素
@property(readonly,getter=isEmpty) BOOL empty;
// 和view的bounds是不一樣的,它獲取path的X坐標(biāo)、Y坐標(biāo)、寬度,但是高度為0
@property(nonatomic,readonly) CGRect bounds;
// 當(dāng)前path的位置,可以理解為path的終點
@property(nonatomic,readonly) CGPoint currentPoint;
// 接收器是否包含指定的點
- (BOOL)containsPoint:(CGPoint)point;
// Drawing properties
// path寬度
@property(nonatomic) CGFloat lineWidth;
// path線端樣式,有3種類型枚舉
typedef CF_ENUM(int32_t, CGLineCap) {
kCGLineCapButt,
kCGLineCapRound,
kCGLineCapSquare
};
@property(nonatomic) CGLineCap lineCapStyle;
lineCapStyle:path.png
@property(nonatomic) CGLineJoin lineJoinStyle;
/* Line join styles. */
typedef CF_ENUM(int32_t, CGLineJoin) {
kCGLineJoinMiter,
kCGLineJoinRound,
kCGLineJoinBevel
};
lineJoinStyle.png
// 最大斜接長度(只有在使用kCGLineJoinMiter是才有效), 邊角的角度越小,斜接長度就會越大
@property(nonatomic) CGFloat miterLimit; // Used when lineJoinStyle is kCGLineJoinMiter
// 彎曲路徑的渲染精度,默認(rèn)為0.6,越小精度越高,相應(yīng)的更加消耗性能。
@property(nonatomic) CGFloat flatness;
// 一個bool值 指定even-odd規(guī)則是否在path可用
@property(nonatomic) BOOL usesEvenOddFillRule; // Default is NO. When YES, the even-odd fill rule is used for drawing, clipping, and hit testing.
// 繪制虛線
- (void)setLineDash:(nullable const CGFloat *)pattern count:(NSInteger)count phase:(CGFloat)phase;
// 檢索線型
- (void)getLineDash:(nullable CGFloat *)pattern count:(nullable NSInteger *)count phase:(nullable CGFloat *)phase;
// Path operations on the current graphics context
// 填充顏色
- (void)fill;
// 描邊,路徑創(chuàng)建需要描邊才能顯示出來
- (void)stroke;
//設(shè)置描邊顏色,需要在設(shè)置后調(diào)用描邊方法:
[[UIColor blackColor] setStroke];
// 設(shè)置填充顏色,需要在設(shè)置后調(diào)用填充方法
[[UIColor redColor] setFill];
// These methods do not affect the blend mode or alpha of the current graphics context
// 用指定的混合模式和透明度值來描繪受接收路徑所包圍的區(qū)域
- (void)fillWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
// 使用指定的混合模式和透明度值沿著接收器路徑。繪制一行
- (void)strokeWithBlendMode:(CGBlendMode)blendMode alpha:(CGFloat)alpha;
// 修改當(dāng)前圖形上下文的繪圖區(qū)域可見,隨后的繪圖操作導(dǎo)致呈現(xiàn)內(nèi)容只有發(fā)生在指定路徑的填充區(qū)域
- (void)addClip;