iOS開發 貝塞爾曲線UIBezierPath

使用UIBezierPath類能夠建立基於矢量的路徑,這個類在UIKit中。此類是Core Graphics框架關於path的一個封裝。使用此類能夠定義簡單的形狀,如橢圓或者矩形,或者有多個直線和曲線段組成的形狀。框架

    
1.Bezier Path 基礎
   UIBezierPath對象是CGPathRef數據類型的封裝。path若是是基於矢量形狀的,都用直線和曲線段去建立。咱們使用直線段去建立矩形和多邊形,使用曲線段去建立弧(arc),圓或者其餘複雜的曲線形狀。每一段都包括一個或者多個點,繪圖命令定義如何去詮釋這些點。每個直線段或者曲線段的結束的地方是下一個的開始的地方。每個鏈接的直線或者曲線段的集合成爲subpath。一個UIBezierPath對象定義一個完整的路徑包括一個或者多個subpaths。
 
   建立和使用一個path對象的過程是分開的。建立path是第一步,包含一下步驟:
(1)建立一個Bezier path對象。
(2)使用方法moveToPoint:去設置初始線段的起點。
(3)添加line或者curve去定義一個或者多個subpaths。
(4)改變UIBezierPath對象跟繪圖相關的屬性。
例如,咱們能夠設置stroked path的屬性lineWidth和lineJoinStyle。也能夠設置filled path的屬性usesEvenOddFillRule。
 
   當建立path,咱們應該管理path上面的點相對於原點(0,0),這樣咱們在隨後就能夠很容易的移動path了。爲了繪製path對象,咱們要用到stroke和fill方法。這些方法在current graphic context下渲染path的line和curve段。
 
 
二、使用UIBezierPath建立多邊形---在path下面添加直線條造成多邊形
多邊形是一些簡單的形狀,這些形狀是由一些直線線條組成,咱們能夠用moveToPoint: 和 addLineToPoint:方法去構建。
方法moveToPoint:設置咱們想要建立形狀的起點。從這點開始,咱們能夠用方法addLineToPoint:去建立一個形狀的線段。
咱們能夠連續的建立line,每個line的起點都是先前的終點,終點就是指定的點。
 
下面的代碼描述瞭如何用線段去建立一個五邊形。第五條線經過調用closePath方法獲得的,它鏈接了最後一個點(0,40)和第一個點(100,0)
說明:closePath方法不只結束一個shape的subpath表述,它也在最後一個點和第一個點之間畫一條線段,若是咱們畫多邊形的話,這個一個便利的方法咱們不須要去畫最後一條線。
 
  1. // Only override drawRect: if you perform custom drawing.  
  2. // An empty implementation adversely affects performance during animation.  
  3. - (void)drawRect:(CGRect)rect  
  4. {  
  5.     UIColor *color = [UIColor redColor];  
  6.     [color set]; //設置線條顏色  
  7.       
  8.     UIBezierPath* aPath = [UIBezierPath bezierPath];  
  9.     aPath.lineWidth = 5.0;  
  10.       
  11.     aPath.lineCapStyle = kCGLineCapRound; //線條拐角  
  12.     aPath.lineJoinStyle = kCGLineCapRound; //終點處理  
  13.       
  14.     // Set the starting point of the shape.  
  15.     [aPath moveToPoint:CGPointMake(100.0, 0.0)];  
  16.       
  17.     // Draw the lines  
  18.     [aPath addLineToPoint:CGPointMake(200.0, 40.0)];  
  19.     [aPath addLineToPoint:CGPointMake(160, 140)];  
  20.     [aPath addLineToPoint:CGPointMake(40.0, 140)];  
  21.     [aPath addLineToPoint:CGPointMake(0.0, 40.0)];  
  22.     [aPath closePath];//第五條線經過調用closePath方法獲得的  
  23.       
  24.     [aPath stroke];//Draws line 根據座標點連線  
  25. }  
注:這個類要繼承自UIView。
 
運行的結果以下圖:
 
若是修改最後一句代碼:[aPath fill];
運行結果就以下:
 
這樣就知道stroke  和  fill  方法的區別了吧!
 
三、使用UIBezierPath建立矩形
使用這個方法便可:
  1. Creates and returns a new UIBezierPath object initialized with a rectangular path.  
  2.   
  3. + (UIBezierPath *)bezierPathWithRect:(CGRect)rect  

demo代碼:
  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     UIColor *color = [UIColor redColor];  
  4.     [color set]; //設置線條顏色  
  5.       
  6.     UIBezierPath* aPath = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 100, 50)];  
  7.       
  8.     aPath.lineWidth = 5.0;  
  9.     aPath.lineCapStyle = kCGLineCapRound; //線條拐角  
  10.     aPath.lineJoinStyle = kCGLineCapRound; //終點處理  
  11.       
  12.     [aPath stroke];  
  13. }  

四、使用UIBezierPath建立圓形或者橢圓形
使用這個方法便可:
  1. Creates and returns a new UIBezierPath object initialized with an oval path inscribed in the specified rectangle  
  2.   
  3. + (UIBezierPath *)bezierPathWithOvalInRect:(CGRect)rect  

這個方法根據傳入的rect矩形參數繪製一個內切曲線。
當傳入的rect是一個正方形時,繪製的圖像是一個內切圓;當傳入的rect是一個長方形時,繪製的圖像是一個內切橢圓。
 
五、使用UIBezierPath建立一段弧線
使用這個方法:
 
  1. Creates and returns a new UIBezierPath object initialized with an arc of a circle.  
  2.   
  3. + (UIBezierPath *)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise  
  4. Parameters  
  5. center  
  6. Specifies the center point of the circle (in the current coordinate system) used to define the arc.  
  7. radius  
  8. Specifies the radius of the circle used to define the arc.  
  9. startAngle  
  10. Specifies the starting angle of the arc (measured in radians).  
  11. endAngle  
  12. Specifies the end angle of the arc (measured in radians).  
  13. clockwise  
  14. The direction in which to draw the arc.  
  15. Return Value  
  16. new path object with the specified arc.  
 
其中的參數分別指定:這段圓弧的中心,半徑,開始角度,結束角度,是否順時針方向。
 
下圖爲弧線的參考系。
 


demo代碼:
  1. #define pi 3.14159265359  
  2. #define   DEGREES_TO_RADIANS(degrees)  ((pi * degrees)/ 180)  
 
  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     UIColor *color = [UIColor redColor];  
  4.     [color set]; //設置線條顏色  
  5.       
  6.     UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)  
  7.                                                          radius:75  
  8.                                                      startAngle:0  
  9.                                                        endAngle:DEGREES_TO_RADIANS(135)  
  10.                                                       clockwise:YES];  
  11.       
  12.     aPath.lineWidth = 5.0;  
  13.     aPath.lineCapStyle = kCGLineCapRound; //線條拐角  
  14.     aPath.lineJoinStyle = kCGLineCapRound; //終點處理  
  15.       
  16.     [aPath stroke];  
  17. }  
 
結果以下圖:
 
六、UIBezierPath類提供了添加二次貝塞爾曲線和三次貝塞爾曲線的支持。
曲線段在當前點開始,在指定的點結束。曲線的形狀有開始點,結束點,一個或者多個控制點的切線定義。下圖顯示了兩種曲線類型的類似,以及控制點和curve形狀的關係。
 
(1)繪製二次貝塞爾曲線
 
使用到這個方法:
  1. Appends a quadratic Bézier curve to the receiver’s path.  
  2.   
  3. - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint  
  4. Parameters  
  5. endPoint  
  6. The end point of the curve.  
  7. controlPoint  
  8. The control point of the curve.  

 
demo代碼:
  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     UIColor *color = [UIColor redColor];  
  4.     [color set]; //設置線條顏色  
  5.       
  6.     UIBezierPath* aPath = [UIBezierPath bezierPath];  
  7.       
  8.     aPath.lineWidth = 5.0;  
  9.     aPath.lineCapStyle = kCGLineCapRound; //線條拐角  
  10.     aPath.lineJoinStyle = kCGLineCapRound; //終點處理  
  11.       
  12.     [aPath moveToPoint:CGPointMake(20, 100)];  
  13.       
  14.     [aPath addQuadCurveToPoint:CGPointMake(120, 100) controlPoint:CGPointMake(70, 0)];  
  15.       
  16.     [aPath stroke];  
  17. }  

 
 
(2)繪製三次貝塞爾曲線
 
使用到這個方法:
  1. Appends a cubic Bézier curve to the receiver’s path.  
  2.   
  3. - (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2  
  4. Parameters  
  5. endPoint  
  6. The end point of the curve.  
  7. controlPoint1  
  8. The first control point to use when computing the curve.  
  9. controlPoint2  
  10. The second control point to use when computing the curve.  

 
demo代碼:
  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     UIColor *color = [UIColor redColor];  
  4.     [color set]; //設置線條顏色  
  5.       
  6.     UIBezierPath* aPath = [UIBezierPath bezierPath];  
  7.       
  8.     aPath.lineWidth = 5.0;  
  9.     aPath.lineCapStyle = kCGLineCapRound; //線條拐角  
  10.     aPath.lineJoinStyle = kCGLineCapRound; //終點處理  
  11.       
  12.     [aPath moveToPoint:CGPointMake(20, 50)];  
  13.       
  14.     [aPath addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100)];  
  15.       
  16.     [aPath stroke];  
  17. }  

 
 
7.使用Core Graphics函數去修改path。
    UIBezierPath類只是CGPathRef數據類型和path繪圖屬性的一個封裝。雖然一般咱們能夠用UIBezierPath類的方法去添加直線段和曲線段,UIBezierPath類還提供了一個屬性CGPath,咱們能夠用來直接修改底層的path data type。若是咱們但願用Core Graphics 框架函數去建立path,則咱們要用到此屬性。
 
    有兩種方法能夠用來修改和UIBezierPath對象相關的path。能夠徹底的使用Core Graphics函數去修改path,也可使用Core Graphics函數和UIBezierPath函數混合去修改。第一種方法在某些方面相對來講比較容易。咱們能夠建立一個CGPathRef數據類型,並調用咱們須要修改path信息的函數。
下面的代碼就是賦值一個新的CGPathRef給UIBezierPath對象。
// Create the path data
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(0, 0, 300, 300));
CGPathAddEllipseInRect(cgPath, NULL, CGRectMake(50, 50, 200, 200));
 
// Now create the UIBezierPath object
UIBezierPath* aPath = [UIBezierPath bezierPath];
aPath.CGPath = cgPath;
aPath.usesEvenOddFillRule = YES;
 
// After assigning it to the UIBezierPath object, you can release
// your CGPathRef data type safely.
CGPathRelease(cgPath);
     若是咱們使用Core Graphics函數和UIBezierPath函數混合方法,咱們必須當心的移動path 信息在二者之間。由於UIBezierPath類擁有本身底層的CGPathRef data type,咱們不能簡單的檢索該類型並直接的修改它。相反,咱們應該生成一個副本,而後修改此副本,而後賦值此副本給CGPath屬性,以下代碼:

 Mixing Core Graphics and UIBezierPath callside

UIBezierPath*    aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 300, 300)];
 
// Get the CGPathRef and create a mutable version.
CGPathRef cgPath = aPath.CGPath;
CGMutablePathRef  mutablePath = CGPathCreateMutableCopy(cgPath);
 
// Modify the path and assign it back to the UIBezierPath object
CGPathAddEllipseInRect(mutablePath, NULL, CGRectMake(50, 50, 200, 200));
aPath.CGPath = mutablePath;
 
// Release both the mutable copy of the path.
CGPathRelease(mutablePath);
 
8.rendering(渲染)Bezier Path對象的內容。
   當建立一個UIBezierPath對象以後,咱們可使用它的stroke和fill方法在current graphics context中去渲染它。在調用這些方法以前,咱們要進行一些其餘的任務去確保正確的繪製path。
   使用UIColor類的方法去stroke和fill想要的顏色。
   設置形狀在目標視圖中的位置。若是咱們建立的path相對於原點(0,0),則咱們能夠給current drawing context應用一個適當的affie transform。例如,我想drawing一個形狀起始點在(0,0),我能夠調用函數CGContextTranslateCTM,並指定水平和垂直方向的translation值爲10。調整graphic context相對於調整path對象的points是首選的方法,由於咱們能夠很容易的保存和撤銷先前的graphics state。
    更新path對象的drawing 屬性。當渲染path時,UIBezierPath實例的drawing屬性會覆蓋graphics context下的屬性值。
 
   下面的代碼實現了在一個自定義view中實現drawRect:方法中去繪製一個橢圓。橢圓邊框矩形的左上角位於視圖座標系統的點(50,50)處。

  Drawing a path in a view函數

- (void)drawRect:(CGRect)rect
{
    // Create an oval shape to draw.
    UIBezierPath* aPath = [UIBezierPath bezierPathWithOvalInRect:
                                CGRectMake(0, 0, 200, 100)];
 
    // Set the render colors
    [[UIColor blackColor] setStroke];
    [[UIColor redColor] setFill];
 
    CGContextRef aRef = UIGraphicsGetCurrentContext();
 
    // If you have content to draw after the shape,
    // save the current state before changing the transform
    //CGContextSaveGState(aRef);
 
    // Adjust the view's origin temporarily. The oval is
    // now drawn relative to the new origin point.
    CGContextTranslateCTM(aRef, 50, 50);
 
    // Adjust the drawing options as needed.
    aPath.lineWidth = 5;
 
    // Fill the path before stroking it so that the fill
    // color does not obscure the stroked line.
    [aPath fill];
    [aPath stroke];
 
    // Restore the graphics state before drawing any other content.
    //CGContextRestoreGState(aRef);
}

 

另外.....spa

個人願望是.......code

世界和平.........orm

相關文章
相關標籤/搜索