ios - UIBezierPath bezierPathWithArcCenter 未正确居中

标签 ios objective-c uibezierpath

我有一个 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;
}

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

我做错了什么?

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

最佳答案

不要使用 View 的中心!

在我的例子中,使用 ,

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

中心点解决了我的问题。

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

相关文章:

ios - 如何在 SceneKit 中加载多个对象?

ios - 整体加入了UIBezierPath笔画

javascript - 如何使网页在移动设备上保持事件状态?

android - 如何将应用程序本地存储同步到数据库?

ios - 如何从 Parse 中获取多个对象以保存在 Swift 中的 Local DataStorage 中?

objective-c - 以编程方式扩展 NSOutlineView

ios - iOS 中的设备兼容性问题

iphone - 在 cocoa 中的字符串上使用 MD5 哈希?

ios - 如何为签名绘制平滑的 UIBezierPath?

ios - 未添加到 x 时创建三角形 View 失败 : 0 and y: 0