I'm trying to create a simple donut and add possibly a gradient inside of it depending on how it looks. I've seen a couple examples using Core Graphics. I was wondering how it can be done using UIKit with the usesEvenOddFillRule. So far I've created two basic paths for an inner and outer circle and set the usesEvenOddFillRule to YES. But the donut is still filled all the way. Am I missing something simple here?
http://stackoverflow.com/questions/24598045/usesevenoddfillrule-example-for-a-donut-with-uikit
为什么在使用UIBezierPath和CAShapeLayer 时设置 UIBezierPath的 usesEvenOddFillRule 属性对 CAShapeLayer最终的效果没有任何影响?
这时因为 UIBezierPath 最终给 CAShapeLayer 赋值的是CGpathRef 而CGpathRe本身并不包含 任何usesEvenOddFillRule属性,这样 path 设置的路径判断规则对 CAShapeLayer 是没有任何效果的
usesEvenOddFillRule example for a donut with UIKit
0 |
|
CAShapeLayer *layer = (CAShapeLayer *)self.layer;
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
UIBezierPath *outerCircle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)];
[outerCircle setLineWidth:2.0];
[bezierPath appendPath:outerCircle];
UIBezierPath *innerCircle = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 20, 20)];
[innerCircle setLineWidth:2.0];
[bezierPath appendPath:innerCircle];
bezierPath.usesEvenOddFillRule = YES;
layer.path = bezierPath.CGPath;
layer.fillColor = [UIColor orangeColor].CGColor;
layer.strokeColor = [UIColor orangeColor].CGColor;
You are creating your UIBezierPath
just fine.
Check out fillRule
property of CAShapeLayer
. You should be setting it to kCAFillRuleEvenOdd
.
Notice that CAShapeLayer
is taking a CGPathRef
and not a UIBezierPath
, and CGPathRef
does not carry fill rule information with it.