我有一个类型为[UIBezierPath]的数组,我将其转换为cgPaths,然后动画到CAShapeLayer上,我称之为shapeLayer。由于某种原因,我的所有路径都是颠倒的,所以所有路径都被颠倒了。我怎么能解决这个问题,我尝试了几种方法,但遗憾的是没有一种方法可行......但我确实想弄清楚如何扩展路径。这是我绘制swiftPath的代码,swiftPath是一个由函数swiftBirdForm()下的Forms类中的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($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)
}
答案 0 :(得分:1)
使用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