相关疑难解决方法(0)

如何用CAShapeLayer和UIBezierPath绘制一个光滑的圆?

我试图通过使用CAShapeLayer并在其上设置圆形路径来绘制一个描边圆.但是,这种方法在渲染到屏幕时的效果始终低于使用borderRadius或直接在CGContextRef中绘制路径.

以下是所有三种方法的结果: 在此输入图像描述

请注意,第三个渲染效果不佳,尤其是在顶部和底部的笔划内.

将该contentsScale属性设置为[UIScreen mainScreen].scale.

这是我对这三个圆圈的绘图代码.使CAShapeLayer顺利绘制的缺失是什么?

@interface BCViewController ()

@end

@interface BCDrawingView : UIView

@end

@implementation BCDrawingView

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = nil;
        self.opaque = YES;
    }

    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    [[UIColor whiteColor] setFill];
    CGContextFillRect(UIGraphicsGetCurrentContext(), rect);

    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), NULL);
    [[UIColor redColor] setStroke];
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
    [[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)] stroke];
}

@end

@interface BCShapeView : UIView

@end

@implementation BCShapeView

+ (Class)layerClass
{
    return [CAShapeLayer class];
} …
Run Code Online (Sandbox Code Playgroud)

core-graphics calayer uikit cashapelayer ios

74
推荐指数
2
解决办法
4万
查看次数

标签 统计

calayer ×1

cashapelayer ×1

core-graphics ×1

ios ×1

uikit ×1