iOS:UIBezierPath和CAShapeLayer fillRule [英] iOS: UIBezierPath and CAShapeLayer fillRule
问题描述
我之前使用了 UIBezierPath
和 CAShapeLayer
。但几乎每次都与填充路径中包含的对象一起使用颜色。但我想这次填充 UIBezierPath
所包含的对象之外的颜色。
I have used both UIBezierPath
and CAShapeLayer
before. But almost every time in conjunction with filling the object contained within the path with color inside. But I would like this time to fill the color outside of the object contained by the UIBezierPath
.
我刚刚编写并运行以下简单代码,试图让自己熟悉 fillRule
属性:
I just wrote and ran the following simple code trying to get myself acquainted with the fillRule
property:
CAShapeLayer *myLayer = (CAShapeLayer*) self.layer; //size: 320 X 480
UIBezierPath *testPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){{100, 100}, 100, 100}]; //a simple circle
myLayer.fillRule = kCAFillRuleNonZero; // have tried this as well: kCAFillRuleEvenOdd;
myLayer.path = testPath.CGPath;
myLayer.fillColor = [UIColor whiteColor].CGColor;
但内部仍然填充了颜色。我想知道的是,我怎样才能填充路径之外的颜色?如果我在这里使用 fillRule
错误,我想知道是否有其他方法可以实现此目的。提前致谢。
But the color is filled nonetheless inside. What I would like to find out is, how can I fill the color outside of the path? If I am using fillRule
wrong here, I would like to know if there is other methods that can achieve this. Thanks in advance.
推荐答案
主要问题是你无法真正填充形状的外部,因为没有通用的方法来定义这意味着什么。您需要做的是首先在您的形状的外部周围绘制一条路径,然后将该圆圈添加为子路径。如何执行此操作取决于您要使用的填充规则。 EvenOdd是最简单的:
The main problem is that you can't really fill the outside of a shape, since there's no generic way to define what that means. What you need to do is first plot a path around the "outside" of your shape, and then add the circle as a subpath. How you do that depends on which fill rule you want to use. EvenOdd is the easiest:
CAShapeLayer *myLayer = (CAShapeLayer*) self.layer;
UIBezierPath *testPath = [UIBezierPath bezierPathWithRect:self.bounds];
[testPath appendPath:[UIBezierPath bezierPathWithOvalInRect:(CGRect){{100, 100}, 100, 100}]];
myLayer.fillRule = kCAFillRuleEvenOdd;
myLayer.path = testPath.CGPath;
myLayer.fillColor = [UIColor whiteColor].CGColor;
NonZero有点难,因为你必须迫使路径逆时针方向,这不是'大多数UIBezierPath便捷方法的选项:
NonZero is a little bit harder because you have to force the path to be counter-clockwise which isn't an option for most of the UIBezierPath convenience methods:
CAShapeLayer *myLayer = (CAShapeLayer*) self.layer;
UIBezierPath *testPath = [UIBezierPath bezierPathWithRect:self.bounds];
UIBezierPath *counterClockwise = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:M_PI clockwise:NO];
[counterClockwise appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:100 startAngle:M_PI endAngle:0 clockwise:NO]];
[testPath appendPath:counterClockwise];
myLayer.fillRule = kCAFillRuleNonZero;
myLayer.path = testPath.CGPath;
myLayer.fillColor = [UIColor redColor].CGColor;
根据您构建实际路径的方式,它可能无论如何都无济于事。
Depending on how you're constructing your actual path it may not make a difference either way.
如果您还没有看到它,缠绕规则文档有一些很好的图表,我认为很有用。
If you haven't seen it already, the winding rules documentation has some nice diagrams that I find helpful.
这篇关于iOS:UIBezierPath和CAShapeLayer fillRule的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!