doucumet说CAShapLayer是抗锯齿的,为什么得到这个结果。我还在drawRect方法中用CGContext画了一个圆圈,它非常完美。
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];
根据the UIBezierPath docs,bezierPathWithOvalInRect:
“使用一系列Bézier曲线创建了一个近似椭圆形的封闭子路径”,因此它可能无法绘制出完美的圆形。
如果使用UIBezierPath绘制圆圈将不是一个完美的圆,因为它是Bézier曲线的近似值。
你可以从一个方形的UIView画出一个完美的圆圈。例如:
myView.layer.cornerRadius = myView.frame.size.width/2;
在本主题中解释了绘制圆圈的三种方法 - > How to draw a smooth circle with CAShapeLayer and UIBezierPath?
iOS 12:我在iPhone XS和XS Max上遇到了同样的问题。我不知道为什么,但在iPhone 8上没有问题。圆圈是完美的圆形。
为了解决XS和XS Max上的问题,我将flatness
的UIBezierPath
属性设置为0.0,并确保用于UIBezierPath
的矩形的中心是整数或浮点数,例如: 25.5。我的矩形中心是y = 51.166666,这导致椭圆形不圆的问题。