iOS Learning Note: Use Cashapelayer to create a matte layer with a hollow area

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.