我在CAShapeLayer的12点钟位置创建了一个弧形,并将其作为子图层添加到UIView上。电弧粗糙或凹凸不平。我尝试了抗锯齿,角半径,线连接,并且还尝试过光栅化但没有运气。任何想法为什么它不顺畅,怎么做才能让它更顺畅?如果在9点钟或3点钟处制作相同的弧线,则它们是平滑的,但不是12点钟或6点钟。感谢任何帮助。
以下是代码:
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, nil, 38.25, 38.25, 25, degreesToRadians(304), degreesToRadians(236), YES);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setAllowsEdgeAntialiasing:YES];
[shapeLayer setEdgeAntialiasingMask:kCALayerLeftEdge | kCALayerRightEdge | kCALayerBottomEdge | kCALayerTopEdge];
[shapeLayer setCornerRadius:2.0];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setStrokeColor:[color CGColor]];
[shapeLayer setLineCap:kCALineCapRound];
[shapeLayer setLineWidth:3.0];
[shapeLayer setPath:path];
这是弧线的链接。顶部和底部不像左边那样光滑。它们在设备上看起来不太流畅。屏幕截图比实际更平滑了它们。 http://oi57.tinypic.com/2wf4i9c.jpg
答案 0 :(得分:0)
看起来您的图层不在像素范围内。也就是说,它会对图层进行栅格化,然后在绘制之前将其移动一小部分像素。我的第一个建议是将这些x.25值舍入为整数。
顺便说一下,为什么你在304, 236
中将startAngle
和endAngle
作为CGPathAddArc()
传递?这些参数是弧度,而不是度数。
edgeAntialiasingMask
和allowsEdgeAntialiasing
与图层边缘的渲染方式有关,不应影响内容。
编辑:差异很微妙,但当我停止使用小数值时,线条中的轻微颠簸就会消失。顺便说一句,如果你以.png格式发布你的图像,就会更容易看到发生了什么,因为它们没有从jpg压缩中获得伪像。
// Fractional values
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddArc(path, nil, 138.25, 138.25, 25, degreesToRadians(304), degreesToRadians(236), YES);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setStrokeColor:[UIColor blueColor].CGColor];
[shapeLayer setLineCap:kCALineCapRound];
[shapeLayer setLineWidth:3.0];
[shapeLayer setPath:path];
CGPathRelease(path);
[self.view.layer addSublayer:shapeLayer];
// Integral values
CGMutablePathRef newPath = CGPathCreateMutable();
CGPathAddArc(newPath, nil, 200, 138, 25, degreesToRadians(304), degreesToRadians(236), YES);
CAShapeLayer *newShapeLayer = [CAShapeLayer layer];
[newShapeLayer setLineJoin:kCALineJoinRound];
[newShapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[newShapeLayer setStrokeColor:[UIColor blueColor].CGColor];
[newShapeLayer setLineCap:kCALineCapRound];
[newShapeLayer setLineWidth:3.0];
[newShapeLayer setPath:newPath];
CGPathRelease(newPath);
[self.view.layer addSublayer:newShapeLayer];
结果:
当我在Photoshop中以差异模式叠加它们时,您可以看到像素的不同之处: