我都用过UIBezierPath
and CAShapeLayer
前。但几乎每次都会结合用内部颜色填充路径中包含的对象。但我想这次填充包含的对象外部的颜色UIBezierPath
.
我刚刚编写并运行了以下简单的代码,试图让自己熟悉fillRule
财产:
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
这里错了,我想知道是否有其他方法可以实现这一点。提前致谢。
主要问题是你无法真正填写outside形状,因为没有通用的方法来定义它的含义。您需要做的是首先围绕形状的“外部”绘制一条路径,然后将圆添加为子路径。如何执行此操作取决于您要使用哪种填充规则。 EvenOdd 是最简单的:
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 便捷方法来说不是一个选项:
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;
根据您构建实际路径的方式,这两种方式可能都不会产生影响。
如果您还没有看过,缠绕规则文档 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Paths/Paths.html#//apple_ref/doc/uid/TP40003290-CH206-BAJIJJGD有一些我觉得很有帮助的漂亮图表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)