I want to do something similar to Photoshops inner shadow effect in Core Graphics. If I draw/fill a path with this effect, I want get something similar to the following:
Here are the layers you need to create to make this image, from back to front:
The base color, in this case a white background.
The shadow.
The shape casting the shadow. This is made by finding the bounding box of the inner shape, expanding that box by more than the width of the shadow, then cutting a hole in the box with the inner shape.
Clipping these with the inner shape.
Then finally drawing the surrounding colored shape, in this case a rectangle with the inner shape cut out.
Note: Depending upon the expected look, the shape casting the shadow may or may not be the same shape filling the foreground color. A thin section between the inner shape and the outer shape would cast a reduced shadow. If that effect is not desired, a larger outer shape would be required to get the consistent inner shadow. Also, the explicit clipping of the shadow is required in case the shadow extends beyond the outer shape.
To draw a shape with a hole in the middle, like this example shape, you'll want to draw a path with two subpaths. One subpath would be the outer box, and the other would be the inner irregular shape. If you're using the default nonzero winding number rule, you'll want to specify the points for the outer box in the opposite direction than the inner irregular shape. For example, specifying the outer box's points in clockwise order would require specifying the inner shape's points in counter-clockwise order. Refer to the Quartz 2D Programmer's Guide's section on Paths for more details.
发布评论
评论(2)
以下是您需要创建的用于制作此图像的图层(从后到前):
基色,在本例中为白色背景。
影子。
投射阴影的形状。这是通过找到内部形状的边界框,将该框扩展超过阴影的宽度,然后在具有内部形状的框上切一个孔来实现的。
用内部形状剪掉它们。
然后最后绘制周围的彩色形状,在本例中是一个内部形状被剪掉的矩形。
注意:根据预期的外观,投射阴影的形状可能与填充前景色的形状相同,也可能不同。内部形状和外部形状之间的薄部分会减少阴影。如果不需要这种效果,则需要更大的外部形状才能获得一致的内部阴影。此外,如果阴影超出外部形状,则需要对阴影进行显式剪切。
要绘制中间有孔的形状(如本示例形状),您需要绘制带有两个子路径的路径。一个子路径是外部盒子,另一个子路径是内部不规则形状。如果您使用默认的非零缠绕数规则,则需要在与内部不规则形状相反的方向上指定外部框的点。例如,按顺时针顺序指定外部框的点需要按逆时针顺序指定内部形状的点。请参阅Quartz 2D 程序员指南中有关路径的部分 了解更多详细信息。
Here are the layers you need to create to make this image, from back to front:
The base color, in this case a white background.
The shadow.
The shape casting the shadow. This is made by finding the bounding box of the inner shape, expanding that box by more than the width of the shadow, then cutting a hole in the box with the inner shape.
Clipping these with the inner shape.
Then finally drawing the surrounding colored shape, in this case a rectangle with the inner shape cut out.
Note: Depending upon the expected look, the shape casting the shadow may or may not be the same shape filling the foreground color. A thin section between the inner shape and the outer shape would cast a reduced shadow. If that effect is not desired, a larger outer shape would be required to get the consistent inner shadow. Also, the explicit clipping of the shadow is required in case the shadow extends beyond the outer shape.
To draw a shape with a hole in the middle, like this example shape, you'll want to draw a path with two subpaths. One subpath would be the outer box, and the other would be the inner irregular shape. If you're using the default nonzero winding number rule, you'll want to specify the points for the outer box in the opposite direction than the inner irregular shape. For example, specifying the outer box's points in clockwise order would require specifying the inner shape's points in counter-clockwise order. Refer to the Quartz 2D Programmer's Guide's section on Paths for more details.
石英中的插入/内部投影
将此代码放入 xcode Playground 中,您就可以开始了:
https:/ /gist.github.com/eonist/520fa35958c123ad6840
Inset/Inner Drop-shadow in quartz
Drop this code in xcode playground and you are on your way:
https://gist.github.com/eonist/520fa35958c123ad6840