IOS uses CAShapeLayer’s FillRule property to generate a layer with a hollow mask

1. hollow rectangle:
sets the intersection of the two Rect by setting the fill rule kCAFillRuleEvenOdd

UIView* aView = [[UIView alloc] initWithFrame:CGRectMake (100,80, 100, 100)]; [self.view addSubview:aView]; / / used to identify layer drawing is correct aView.layer.borderWidth = 1; aView.layer.borderColor = [UIColor blackColor].CGColor; CAShapeLayer* cropLayer = [[CAShapeLayer alloc] init]; [aView.layer addSublayer:cropLayer]; / / create a CGMutablePathRef path (=CGPathCreateMutable) draw a path; / / rect CGRect cropRect hollow rectangle = CGRectMake (20, 30, 60, 40); / / CGPathAddRect (path, nil drawing rect, aView.bounds); CGPathAddRect (path, nil, cropRect); / / set the fill rules (key) [cropLayer setFillRule:kCAFillRuleEvenOdd]; path [cropLayer setPath:path] / / drawing Association ; / / set the fill color of the [cropLayer setFillColor:[[UIColor redColor] CGColor]];

Design sketch´╝Ü

IOS uses CAShapeLayer's FillRule property to generate a layer with a hollow mask
1.png

2. hollow circles:
draws the non intersection of Rect and Arc by setting the fill rule as kCAFillRuleEvenOdd

CAShapeLayer *pShapeLayer = [CAShapeLayer layer] = [UIColor; pShapeLayer.fillColor blackColor].CGColor; [self.overlayView.layer addSublayer:pShapeLayer]; UIBezierPath *pPath = [UIBezierPath bezierPathWithArcCenter:self.overlayView.center radius: SCALE_FRAME_Y/2 startAngle:0.0 endAngle:M_PI*2 clockwise:YES]; pShapeLayer.path = pPath.CGPath; UIBezierPath = *pOtherPath [UIBezierPath bezierPathWithRect:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height); pShapeLayer.path = pOtherPath.CGPath; [pOtherPath appendPath:pPath]; pShapeLayer.path = pOtherPath.CGPath; pShapeLayer.fillRule = kCAFillRuleEvenOdd / / key;

Effect drawing:

IOS uses CAShapeLayer's FillRule property to generate a layer with a hollow mask
2.png

Key: understanding filling rules fillRule

The fill rule used when filling / the path. Options are `non-zero'and Defaults to `non-zero'. * / @property * `even-odd'. (copy) NSString *fillRule;

Property is used to specify which algorithm to use to determine whether a region on the canvas belongs to the graph “internal” (the inner area will be filled). For a simple, non intersecting path, which area is “inside” is visually removed. However, for a complex path, such as self intersection or a sub path surrounding another sub path, the understanding of the interior is less clear.

“KCAFillRuleNonZero
” literally means “nonzero.””. According to this rule, it is necessary to judge whether a point is in a graph, a ray in any direction from that point, and then to check the intersection of the ray and the graph path. Start counting from 0, path from left to right, ray count, plus 1, right to left, ray count, minus 1. When the result is counted, if the result is 0, the point is outside the figure, otherwise it is considered inside. The following illustration demonstrates the kCAFillRuleNonZero rule:

IOS uses CAShapeLayer's FillRule property to generate a layer with a hollow mask
3.png

“KCAFillRuleEvenOdd
” literally means “parity.””. According to this rule, it is necessary to determine whether a point is in a graph, a ray in any direction from that point, and then to check the number of intersections between the ray and the graph path. If the result is odd, the point is inside, and even number thinks the point is outside. The following illustration demonstrates the kCAFillRuleEvenOdd rule:

IOS uses CAShapeLayer's FillRule property to generate a layer with a hollow mask
4.png

Text link: http://keepmovingxin.com/archives/21