2013-08-28 57 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?