将outerBelowCircle CAShapeLayer的位置设置在View的中心

问题描述 投票:0回答:1

我使用CAShapeLayer制作一个圆圈,我想在视图的中心设置它的中心。但是圆圈将在中心线下方创建(见截图),有人可以帮我吗?

let circle = CAShapeLayer()
view.layer.addSublayer(circle)
circle.position = view.center


    let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
    circle.path = circularPath.cgPath

我也改变了在中心设置它的方式,但它不起作用

circle.position = CGPoint(x: view.layer.bounds.midX, y: view.layer.bounds.midY)

enter image description here

swift view cashapelayer
1个回答
1
投票

根据您的屏幕截图考虑以下方法,此方法将解决您的问题。

func showCircle() {

    let view = UIView()
    let circle = CAShapeLayer()
    view.layer.addSublayer(circle)
    let positionX = view.center.x
    let positionY = view.center.y - 100 // your circle radius
    circle.position = CGPoint(x: positionX, y:  positionY)


    let circularPath = UIBezierPath(arcCenter: .zero, radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true)
    circle.path = circularPath.cgPath
}

我没有测试过代码。请自行验证。

© www.soinside.com 2019 - 2024. All rights reserved.