UIBezierPath
由于項目需要,用到了貝塞爾曲線,原來也沒有具體的研究,今天有時間研究了下,寫了幾行demo測試了下,以后項目中估計還會用到。
UIBezierPath 是UIKit框架的一個類,繼承于NSObject,是框架Core Graphics對于path的一個OC封裝,使用這個類你可以很簡單的畫矩形、圓形、橢圓、弧線以及你想要的復雜的形狀圖形,來滿足項目中的需求。
先來看下這個類都有什么屬性及方法;
+(instancetype)bezierPath;實例化對象
+(instancetype)bezierPathWithRect:(CGRect)rect; //根據frame創建一個實例對象
+(instancetype)bezierPathWithOvalInRect:(CGRect)rect;//根據frame實例化對象,畫橢圓
+(instancetype)bezierPathWithRoundedRect:(CGRect)rectcornerRadius:(CGFloat)cornerRadius;//根據frame 角半徑來確定一個類似于矩形的形狀 并返回實例對象
+(instancetype)bezierPathWithRoundedRect:(CGRect)rectbyRoundingCorners:(UIRectCorner)cornerscornerRadii:(CGSize)cornerRadii;//根據給定的frame、矩形四個角的具體哪個角以及角的半徑來確定一個實例對象
+(instancetype)bezierPathWithArcCenter:(CGPoint)centerradius:(CGFloat)radiusstartAngle:(CGFloat)startAngleendAngle:(CGFloat)endAngleclockwise:(BOOL)clockwise;//畫橢圓、圓、弧線,并返回實例對象,參數依次是:圓中心點、開始的角度、結束的角度、順逆時針;
+(instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;//根據CGPath實例化對象
@property(nonatomic)CGPathRef? CGPath; //CGPathRef屬性
-(CGPathRef)CGPath
-(void)moveToPoint:(CGPoint)point;//起點
-(void)addLineToPoint:(CGPoint)point;//添加線到具體的某個點
-(void)addCurveToPoint:(CGPoint)endPointcontrolPoint1:(CGPoint)controlPoint1controlPoint2:(CGPoint)controlPoint2;//兩個控制點的曲線線段
-(void)addQuadCurveToPoint:(CGPoint)endPointcontrolPoint:(CGPoint)controlPoint;//一個控制點的曲線
-(void)addArcWithCenter:(CGPoint)centerradius:(CGFloat)radiusstartAngle:(CGFloat)startAngleendAngle:(CGFloat)endAngleclockwise:(BOOL)clockwiseNS_AVAILABLE_IOS(4_0);//畫橢圓、圓、弧線,并返回實例對象,參數依次是:圓中心點、開始的角度、結束的角度、順逆時針;
-(void)closePath;//關閉當前路徑(關閉與不關閉有區別,demo會講到)
-(void)removeAllPoints;//移除所有設置的點
-(void)appendPath:(UIBezierPath*)bezierPath;//沒寫demo測試,有興趣可以試試
-(void)applyTransform:(CGAffineTransform)transform;
-(UIBezierPath*)bezierPathByReversingPath
@property(nonatomic)CGFloat ?lineWidth;//線寬
@property(nonatomic)CGLineCap lineCapStyle;//有三種:1、kCGLineCapSquare 2、kCGLineCapButt 3、kCGLineCapRound
@property(nonatomic)CGLineJoin lineJoinStyle;//有三種://1、kCGLineJoinBevel拐角以斜線結束 2、kCGLineJoinMiter拐角以直角結束 3、kCGLineJoinRound拐角以圓角形式結束
@property(nonatomic)CGFloat miterLimit; //UsedwhenlineJoinStyleiskCGLineJoinMiter
@property(nonatomic)CGFloat flatness;
@property(nonatomic)BOOL usesEvenOddFillRule;
-(void)fill;//填充
-(void)stroke;//畫線
還有幾個方法屬性不常用,就沒有貼上,以上是經常用到的方法和屬性,下面展示demo:
1、劃線
//畫線
UIBezierPath*bezierPath=[UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(10,10)];//起點
[bezierPath addLineToPoint:CGPointMake(10,50)];//加條線,從點移動到另一個點
[bezierPath addLineToPoint:CGPointMake(50,50)];//加條線,從點移動到另一個點
bezierPath.lineJoinStyle=kCGLineJoinRound;
bezierPath.lineCapStyle=kCGLineCapRound;
[bezierPath closePath];//關閉貝塞爾線(是否首尾連接)
UIColor*fillColor=[UIColor greenColor];//設置顏色
[fillColor set];//填充顏色
[bezierPath stroke];//貝塞爾線進行填充
待續......