看core graphics文档的时候,学习到UIBezierPath的一个属性usesEvenOddFillRule,之前一直没用过这个,看了一些资料才知道它是渲染的一种填充方式,系统给我们提供两种方式:NonZero(交点所在路径同向+1,反向-1,最后为0,则位于区域外面,否则就位于区域内部)、EvenOdd(内部一点向外作射线,与路径偶数个交点,则该点在内部,否则在区域外面),通过设置该属性,可以明确填充方式,哪些部分填充,哪些部分不填充。
其他地方已经说得很明白了,我这里就不再赘述了。
这里只是说一下,利用这个属性可以很简单的实现空心圆的绘制,这里通过两种方式。
1. 利用 UIBezierPath 及CAShapeLayer的fillRule属性。
直接上代码吧,很简单。
CAShapeLayer *layer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[pathaddArcWithCenter:CGPointMake(150, 150) radius:25 startAngle:0 endAngle:2*M_PI clockwise:YES];
[pathmoveToPoint:CGPointMake(200, 150)];
[pathaddArcWithCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
layer.path= path.CGPath;
layer.fillColor = [UIColor redColor].CGColor;
layer.strokeColor = [UIColor blackColor].CGColor;
layer.fillRule = kCAFillRuleEvenOdd;
[self.view.layer addSublayer:layer];
但是还有一个疑问,通过设置layer.fillRule可以达到效果,但是如果通过设置path.usesEvenOddFillRule则没有效果,那这个属性到底是怎么用的呢?但是下面的一种方式,通过设置path的这个属性就是可以的,只不过是直接使用core graphics方法。
实现效果如图:

2.自己定义一个view,在实现文件里重写drawRect方法:
@implementation CustomView
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextAddArc(context,50,50,25,0,2*M_PI,0);
CGContextAddArc(context,50,50,50,0,2*M_PI,0);
CGContextDrawPath(context, kCGPathEOFill);
CGContextClosePath(context);
CGContextFillPath(context);
}
同样也可以实现效果:

好了,就到这里吧,如果你们明白我提的那个疑问,还请麻烦告诉我一声哦。
网友评论