iOS UIBezierPath知識介紹


UIBezierPath是在畫圖,定制動畫軌跡中都有應用。        

UIBezierPath有許多類方法,能夠創建基本的曲線,比如利用一個rect創建一個橢圓path的方法:bezierPathWithOvalInRect。

1.看看如何繪制一個扇形路徑 

 UIBezierPath *piePath = [UIBezierPathbezierPath];
 [piePath moveToPoint:center];
 [piePath addArcWithCenter:center  radius:radius  startAngle:topAngle  endAngle:endAngle  clockwise:YES];
 [piePath closePath];

注意,這里的moveToPoint和closePath的使用,沒有這2句話,path僅僅是一段弧,不是一個扇形。

2.利用path進行裁剪

UIBezierPath中的addClip功能:

This method modifies the visible drawing area of the current graphics context. After calling it, subsequent drawing operations result in rendered content only if they occur within the fill area of the specified path.

簡單的說,就是一個path調用addClip之后,它所在的context的可見區域就變成了它的“fill area”,接下來的繪制,如果在這個區域外都會被無視。

比如,如果在上邊的代碼上邊加上一段代碼,就能實現圓弧的繪制,而不是扇形:  

UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:outCircleRect];
UIBezierPath *innerCirclePath = [UIBezierPath bezierPathWithOvalInRect:innerCircleRect];
[circlePath appendPath:innerCirclePath];
[circlePath setUsesEvenOddFillRule:YES];  //后便會有說明
[circlePath addClip];

3.usesEvenOddFillRule 判斷“fill area”(填充區域)的方法

如何判斷一個閉合path的填充區域呢,蘋果通過usesEvenOddFillRule屬性,提供2中方法,

If YES, the path is filled using the even-odd rule. If NO, it is filled using the non-zero rule. Both rules are algorithms to determine which areas of a path to fill with the current fill color. A ray is drawn from a point inside a given region to a point anywhere outside the path’s bounds. The total number of crossed path lines (including implicit path lines) and the direction of each path line are then interpreted as follows:

  • For the even-odd rule, if the total number of path crossings is odd, the point is considered to be inside the path and the corresponding region is filled. If the number of crossings is even, the point is considered to be outside the path and the region is not filled.

  • For the non-zero rule, the crossing of a left-to-right path counts as +1 and the crossing of a right-to-left path counts as -1. If the sum of the crossings is nonzero, the point is considered to be inside the path and the corresponding region is filled. If the sum is 0, the point is outside the path and the region is not filled.

簡單的說下,第一種,even-odd ,判斷某點在不在fill area,需要從這點起,向區域外某一點畫射線,如果射線和奇數條path相交,那么這點是在fill area,反之,不在。

第二種,也是畫射線,根據path的順時針和逆時針個數計算,如果和不為0,就是在fill area,反之,不在。

4.containsPoint:判斷是否包含某點

The receiver contains the specified point if that point is in a portion of a closed subpath that would normally be painted during a fill operation.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM