我有一个类型为[UIBezierPath]的数组,我将其转换为cgPaths,然后在一个称为shapeLayer的CAShapeLayer上设置动画。因为某些原因,我所有的路都是颠倒的,所以所有的路都是颠倒的。我怎么能解决这个问题,我尝试了几种方法,但不幸的是,没有一种有效。。。不过,我确实想好了如何缩放路径。这是我绘制swiftPath的代码,它是由函数swiftBirdForm()下的Forms类中的uibezierraths组成的路径。画这条路很好,我就是想不出怎么把它翻转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($0)
    }

    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

关于swift - 如何将动画的UIBezierPath或cgPath翻转到CAShapeLayer上?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49508639/

10-12 07:05