首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SpriteKit -为SKShapeNode形状路径设置动画

SpriteKit -为SKShapeNode形状路径设置动画
EN

Stack Overflow用户
提问于 2018-12-22 01:54:19
回答 1查看 1.3K关注 0票数 7

目前,我正在使用核心图形的形状,并转换成一个不同的形状与CAShapeLayerCABasicAnimation路径动画。

然而,由于游戏的复杂性,如果使用SKShapeNode进入SpritKit,(丢弃CAShapeLayer,使用SKShapeNode),我是否能够平滑地将其动画化为不同的形状?

我没有看到可以让您更改SKShapeNode路径的SKAction方法。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2018-12-25 04:48:44

井。节日快乐。

最简单的方法是使用CALayerAnimation来教SKNode如何像这样操作:

代码语言:javascript
运行
复制
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来了解这个想法(不完全相同,但类似的动画)。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53888993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档