我使用 CoreGraphics 在自定义 SGCircleView 中绘制圆。因为我希望圆圈在被触摸时充满径向渐变,所以我的方法是用原始圆圈 (SGCircleLayer) 绘制一层,然后添加另一层渐变。问题是这些圆圈看起来有些“粗糙”。
这是 SGCircleLayer 中的代码:
- (void) drawLayer: (CALayer *) layer inContext: (CGContextRef) ctx {
UIGraphicsPushContext(ctx);
UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect: CGRectInset(layer.bounds, CLineWidth / 2, CLineWidth / 2)];
self.path = circlePath.CGPath;
[[UIColor whiteColor] setStroke];
circlePath.lineWidth = CLineWidth;
[circlePath stroke];
UIGraphicsPopContext();
}
出于测试目的,我在自定义 SGCircleTestView 的 drawRect 中添加了另一个具有完全相同半径和线宽的圆,它看起来非常平滑。
这是我在 SGCircleTestView(平滑圆)中的 drawRect 的代码:
- (void) drawRect: (CGRect) rect {
CGRect bounds = CGRectInset(rect, CLineWidth * 0.5, CLineWidth * 0.5);
UIBezierPath* circlePath = [UIBezierPath bezierPathWithOvalInRect: bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, CLineWidth2);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
[circlePath stroke];
}
我在绘制 SGCircleLayer 的代码时做错了什么?
最佳答案
您可能需要将图层的 contentsScale 设置为与屏幕相同。
layer.contentsScale = [UIScreen mainScreen].scale;
关于ios - 如何提高CGLayer的绘图质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570432/