Cashapelayer is a special layer used to accept vector path and render directly using the GPU. Look at the following effect:
Corresponding Code:
Let Marklayer = Cashapelayer (); Marklayer.frame = position of self.view.bounds;//layer Marklayer.fillcolor = Uicolor (colorliteralred:0, green:0, blue:0, a lpha:0.3). cgcolor;//the fill color of the layer, here set the transparency marklayer.fillrule = kcafillruleevenodd;//Fill rule, explain let path later Cgpathcreatemutable (); Cgpathaddrect (path, nil, self.subLayer.bounds); Cgpathaddellipseinrect (path, nil, CGRectMake (0, SELF.VIEW.FRAME.SIZE.HEIGHT/2-self.view.frame.size.width, 200)) ; Marklayer.path = path; Set the path to render
Self.view.layer.addSublayer (Marklayer);
We generated a path formed by an ellipse embedded in a rectangle and then rendered by Cashapelayer. The middle ellipse is rendered hollow because we set the FillRule to Kcafillruleevenodd. The following explains the effect of fillrule on usage. Because path can represent very complex shapes, cashapelayer must distinguish between the inner filled area and the outer non-populated area when making a fill. The inner fill area is filled with fill color. Here's how to differentiate:
Select a point from within a region, draw a ray beyond the bounds of path, count the intersection of the ray and path, and then determine whether the area belongs to an internal filled area or an outer non-populated area based on the Fillmode setting.
Fillmode = Kcafillrulenonzero
The intersection of the ray and path is counted, when the ray passes through the path of the line with a left-to-right count of +1, the right-to-left count-1, and the last count as nonzero is the inner path, otherwise external.
Fillmode = kcafillruleevenodd
The number of points of intersection of Ray and path is odd when the path is internal, otherwise it is external.
Back to the beginning of the article, the Middle Oval area of the store-to-path boundary of the ray with the path of the intersection of two (ellipse and rectangular edges), so the ellipse area is an outer non-filled area. The area between the rectangle and the ellipse is the inner filled area.
iOS Learning Note: Use Cashapelayer to create a matte layer with a hollow area