在我的自定义控件中,我定义了一些 CGMutablePathRefs,其中包含绘制控件所需的线条和弧线;一个绘制整体填充形状,其他提供镜面高光。我还定义了两个 CGMutablePathRefs,其中包含作为控件活动和非活动状态的剪切掩码所需的路径。
我正在努力应用剪切路径。我以前使用剪切路径将渐变应用于图像,但这些绘图命令属于 CGContext... 品种,而不是 CGPath... 品种。
出于测试目的,我删除了镜面高光绘图方面,只是试图将大路径剪裁到较小的路径。这是我一直在测试的:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextBeginPath(ctx);
CGContextAddPath(ctx, inactiveClip);
CGContextClosePath(ctx);
CGContextClip(ctx);
CGContextAddPath(ctx, frontFace);
CGContextSetLineWidth(ctx, 1.0);
CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
CGContextFillPath(ctx);
}
通过将剪切命令放在任何绘图之前,我以为我是在对 CoreGraphics 说:“这是你应该实际绘制的区域。”
唉,什么都没画。
所以假设我有倒序我试过:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddPath(ctx, frontFace);
CGContextSetLineWidth(ctx, 1.0);
CGContextSetFillColorWithColor(ctx, [[UIColor blackColor] CGColor]);
CGContextFillPath(ctx);
CGContextBeginPath(ctx);
CGContextAddPath(ctx, inactiveClip);
CGContextClosePath(ctx);
CGContextClip(ctx);
}
这是对 CoreGraphics 说的,“好吧,在你实际着色位之前,对照这个剪辑区域检查它们。”
唉,什么都没有剪裁。
由于我的剪切路径使用一些相同的点和控制点,以相同的顺序,作为填充路径,我还用对 CGContextEOClip 的调用替换了对 CGContextClip 的调用,看看我是否真的在挣扎奇偶规则,但这似乎没有任何视觉影响。
我真的不知道是否需要将 CGContextAddPath 调用与 CGContextBeginPath/CGContextClosePath 调用括起来,但我试图做的是尽量减少 Apple 的示例代码和我的代码之间的差异。在他们的 CGContext 中,他们在 begin/close 调用之间绘制调用,所以我也是。
我在这里有什么误解?