2013-08-28 52 views
2

doucumet说CAShapLayer是反锯齿的,为什么会得到这个结果。 我也在drawRect方法中用CGContext绘制一个圆圈,它非常完美。为什么cashapelayer的圆形路径不那么圆?

UIBezierPath *path = [UIBezierPath bezierPath]; 
    [path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(150, 300, 136, 136)]]; 

    self.circleLayer = [CAShapeLayer layer]; 
    self.circleLayer.path = path.CGPath; 
    self.circleLayer.frame = self.bounds; 
    self.circleLayer.fillColor = [[UIColor whiteColor] colorWithAlphaComponent:0.6].CGColor; 
    [self.layer addSublayer:self.circleLayer]; 

enter image description here

+0

你问我们如何关闭抗锯齿?或者,你的Core Graphics转换也在进行反锯齿,你只是想知道为什么这两者有所不同? – Rob

+0

顺便说一句,我发现,如果你使用'UIBezierPath'方法'addArcWithCenter'具有非整数半径(例如150.2 vs 150),'CAShapeLayer'的反锯齿工件不会分散注意力。 – Rob

回答

2

根据the UIBezierPath docsbezierPathWithOvalInRect:“创建一个封闭子路径即使用贝塞尔曲线的序列近似于椭圆”,所以它可能不绘制正圆。

+0

但[CGContextAddEllipseInRect](https://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGContext/Reference/reference.html#//apple_ref/c/func/CGContextAddEllipseInRect)也表示:“椭圆近似于贝塞尔曲线的序列。“ – Luke

+0

你有什么意见? – Brigham

+1

当我使用CGContextAddEllipseInRect在drawRect中画一个圆时,这是一个完美的圆。 – Luke

1

如果使用UIBezier路径绘制一个圆不会是一个完美的圆,因为它是一个来自Bézier曲线的近似值。

你可以从一个正方形的UIView中绘制一个完美的圆。例如:

myView.layer.cornerRadius = myView.frame.size.width/2;

在本主题中解释了三种方法来绘制一个圆 - >How to draw a smooth circle with CAShapeLayer and UIBezierPath?