草庐IT

ios - UIBezierPath bezierPathWithArcCenter 未正确居中

coder 2023-09-29 原文

我有一个 View ,我想在其中用 UIBezierPath bezierPathWithArcCenter 画一个圆。我的view的initWithFrame如下:

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self == nil) {
        return nil;
    }

    self.circleLayer = [CAShapeLayer layer];
    self.circleLayer.fillColor = UIColor.clearColor.CGColor;
    self.circleLayer.strokeColor = UIColor.blackColor.CGColor;
    self.circleLayer.lineWidth = 4;
    self.circleLayer.bounds = self.bounds;


    self.startAngle = 0.0;
    self.endAngle = M_PI *  2;
    CGPoint center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    CGFloat radius = self.frame.size.height * 0.45;
    self.circleLayer.path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:self.startAngle endAngle:self.endAngle clockwise:YES].CGPath;

    [self.layer addSublayer:self.circleLayer];

    self.backgroundColor = UIColor.greenColor;

    return self;
}

我预计会在我的视野中心产生一个黑色圆圈。然而,实际的输出是这样的:

我做错了什么?

编辑 最奇怪的部分是它在全新安装应用程序后正确绘制 - 任何连续启动都会导致它像所附图像一样绘制。为什么?

最佳答案

不要使用 View 的中心!

在我的例子中,使用 ,

让 centerPoint = CGPoint(x: bounds.midX, y: bounds.midY)

中心点解决了我的问题。

关于ios - UIBezierPath bezierPathWithArcCenter 未正确居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033050/

有关ios - UIBezierPath bezierPathWithArcCenter 未正确居中的更多相关文章

随机推荐