如何翻转动画到 CAShapeLayer 上的 UIBezierPath 或 cgPath?

How do I flip over a UIBezierPath or cgPath thats animated onto the CAShapeLayer?

我有一个 [UIBezierPath] 类型的数组,我将其转换为 cgPaths,然后在我称为 shapeLayer 的 CAShapeLayer 上设置动画。现在由于某种原因我所有的路径都是颠倒的,所以所有的路径都被颠倒了。我该如何解决这个问题,我尝试了几种方法,但遗憾的是 none 其中的方法有效......但是我确实弄清楚了如何缩放路径。这是我绘制 swiftPath 的代码,它是由函数 swiftBirdForm() 下的 Forms class 中的 UIBezierPaths 组成的路径。绘制路径效果很好,我只是不知道如何将其翻转 180 度。

    @objc func drawForm() {
    var swiftPath = Forms.swiftBirdForm()
    let shapeLayer = CAShapeLayer()
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.strokeColor = UIColor.black.cgColor
    shapeLayer.lineWidth = 1
    shapeLayer.frame = CGRect(x: -120, y: 120, width: 350, height: 350)

    var paths: [UIBezierPath] = swiftPath

    guard let path = paths.first else {
        return
    }

    paths.dropFirst()
        .forEach {
            path.append([=10=])
    }

    shapeLayer.transform = CATransform3DMakeScale(0.6, 0.6, 0)
    shapeLayer.path = path.cgPath

    self.view.layer.addSublayer(shapeLayer)

    let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
    strokeEndAnimation.duration = 1.0
    strokeEndAnimation.fromValue = 0.0
    strokeEndAnimation.toValue = 1.0
    shapeLayer.add(strokeEndAnimation, forKey: nil)
}

使用 CATransform3D

shapeLayer.transform = CATransform3DMakeScale(1, -1, 1)

变换路径,

let shapeBounds = shapeLayer.bounds
let mirror = CGAffineTransform(scaleX: 1,
                                  y: -1)
let translate = CGAffineTransform(translationX: 0,
                                  y: shapeBounds.size.height)
let concatenated = mirror.concatenating(translate)

bezierPath.apply(concatenated)

shapeLayer.path = bezierPath.cgPath

变换层,

let shapeFrame = CGRect(x: -120, y: 120, width: 350, height: 350)
let mirrorUpsideDown = CGAffineTransform(scaleX: 1,
                                              y: -1)
shapeLayer.setAffineTransform(mirrorUpsideDown)
shapeLayer.frame = shapeFrame