加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – SpriteKit – Animate SKShapeNode形状路径

发布时间:2020-12-14 17:52:37 所属栏目:百科 来源:网络整理
导读:目前我正在使用Core Graphics作为形状,并使用CAShapeLayer和CABasicAnimation路径动画将形状转换为不同的形状. 但是由于游戏的复杂性,如果要介入SpritKit,使用SKShapeNode(丢弃CAShapeLayer并使用SKShapeNode),我能够将其平滑地设置为不同的形状吗? 我没有
目前我正在使用Core Graphics作为形状,并使用CAShapeLayer和CABasicAnimation路径动画将形状转换为不同的形状.

但是由于游戏的复杂性,如果要介入SpritKit,使用SKShapeNode(丢弃CAShapeLayer并使用SKShapeNode),我能够将其平滑地设置为不同的形状吗?

我没有看到可以让你改变SKShapeNode路径的SKAction方法.

提前致谢.

解决方法

好.节日快乐.

最简单的方法是使用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,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来获取想法(不完全相似但动画相似).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读