在UIBezierPath的两侧应用阴影

Sna*_*ker 5 drawing core-graphics ios uibezierpath

我正在画画.我得到流畅的线条,我可以改变图纸的颜色.但我找不到如何将阴影应用于该线.

为了绘制它,我使用:

[path strokeWithBlendMode:[path blendMode] alpha:1.0];
Run Code Online (Sandbox Code Playgroud)

我看到我可以使用,CGContextSetShadowWithColor()但即使如此,我不知道如何使用它,因为这里是CGPath参考中所说的strokeWithBlendMode:

此方法在绘制之前自动保存当前图形状态,并在完成后恢复该状态,因此您不必自己保存图形状态.

所以CGContextSetShadowWithColor()如果我可以使用它,我真的不知道放在那里或其他任何东西.

问候

小智 3

如果您想使用CGContextSetShadowwithColor(),则需要更改将贝塞尔路径绘制到视图的方式,以便将表示CGPath形式绘制到CGContext. 示例如下:

UIBezierPath *path;     // this is your path as before
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path.CGPath);
CGContextSetLineWidth(context, 2.0);
CGContextSetBlendMode(context, path.blendMode);
CGContextSetShadowWithColor(context, CGSizeMake(1.0, 1.0), 2.0, [UIColor blackColor].CGColor);
CGContextStrokePath(context);
Run Code Online (Sandbox Code Playgroud)

另一种方法是创建一个新CAShapeLayer路径,并通过将其设置为路径属性来绘制该路径。这将很容易地让你添加一个只会遮蔽你的路径的阴影。