目前,我正在使用核心图形的形状,并转换成一个不同的形状与CAShapeLayer
和CABasicAnimation
路径动画。
然而,由于游戏的复杂性,如果使用SKShapeNode
进入SpritKit,(丢弃CAShapeLayer
,使用SKShapeNode
),我是否能够平滑地将其动画化为不同的形状?
我没有看到可以让您更改SKShapeNode
路径的SKAction
方法。
提前谢谢。
发布于 2018-12-24 20:48:44
井。节日快乐。
最简单的方法是使用CALayerAnimation来教SKNode如何像这样操作:
class ViewController: UIViewController {
@IBOutlet weak var skview: SKView!
var path1: CGPath!
var path2: CGPath!
override func viewDidLoad() {
super.viewDidLoad()
path1 = CGPath.init(rect: CGRect.init(x: 0, y: 0, width: 100, height: 100), transform: nil)
path2 = CGPath.init(rect: CGRect.init(x: 0, y: 0, width: 250, height: 400), transform: nil)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let shapeLayer = CAShapeLayer()
self.view.layer.addSublayer(shapeLayer)
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.path = path1
let anim = CABasicAnimation.init(keyPath: "path")
anim.duration = 1.0
anim.fromValue = path1
anim.toValue = path2
anim.isRemovedOnCompletion = false
let shapeNode = SKShapeNode.init(path: path1)
shapeNode.fillColor = UIColor.green
skview.scene?.addChild(shapeNode)
shapeLayer.add(anim, forKey:
"prepanimation")
shapeNode.run(SKAction.customAction(withDuration: anim.duration, actionBlock: { (node, timeDuration) in
(node as! SKShapeNode).path =
shapeLayer.presentation()?.path
}))
}
}
如果你的路径太大,一个最好的方法是考虑将CABasicAnimation转换为CAKeyFrameAnimation方法。
从上面的过程中,您可以在设计时提取一个对( time,presentation_Path)。然后在SKCustomAction中的运行时重新分配。请参考SKKeyframeSequence来了解这个想法(不完全相同,但类似的动画)。
https://stackoverflow.com/questions/53888993
复制相似问题