作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用两种交替颜色(例如黑色和白色,想想 Preview.app 选择框)来描画 CAShapeLayer
的路径。我不确定如何做到这一点,或者这对于 Quartz 是否可行。
我设置了一个 CAShapeLayer 以具有白色边框,然后将路径属性设置为黑色虚线。我希望这能产生黑白虚线的效果。然而,看起来路径是先绘制的,边框是在上面描边的,见截图(毛皮是我的猫的),
任何人都可以提出更好的方法或方法来使其发挥作用吗?
代码,
// Stroke a white border
[shapeLayer setFrame:shapeRect];
[shapeLayer setBorderColor:[[NSColor whiteColor] CGColor]];
[shapeLayer setBorderWidth:1.0];
// Stroked a black dash path (along the border)
// and fill shape with clear colour
[shapeLayer setFillColor:[[NSColor clearColor] CGColor]];
[shapeLayer setStrokeColor:[[NSColor blackColor] CGColor]];
[shapeLayer setLineWidth:1.0];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSNumber numberWithInt:5],
[NSNumber numberWithInt:5],
nil]];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, shapeLayer.bounds);
[shapeLayer setPath:path];
CGPathRelease(path);
最佳答案
我认为这种方法有两个问题(混合图层边框和形状描边)
你可以通过将路径插入到描边宽度的一半来对#1做一些事情
CGFloat halfWidth = 1.0/2.0;
CGPathAddRect(path, NULL, CGRectInset(shapeLayer.bounds, halfWidth, halfWidth));
但是边框仍然会绘制在形状上方(至少我不知道如何更改顺序)。
我认为最简单的解决方案是必须在同一容器层中塑造图层。容器将用于更改框架,两个图层将用于绘图。
底层将有白色描边(无虚线图案),顶层将有黑色虚线描边。由于两个形状具有相同的路径,因此看起来像黑白虚线图案。
这样做也适用于任何形状。
关于objective-c - 如何在 Cocoa 中用两种交替颜色绘制虚线 CGPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18427194/
我是一名优秀的程序员,十分优秀!