我想永远动画2个立方体,直到用户点击其中一个.动画应该像∞一样.
这些是我想要动画的sceneView中的我的立方体:
我试图用嵌套动画做到这一点,它确实部分工作.现在,由于动画总是等到它们在开始新动画之前完成,它看起来并不光滑.
在继续开发∞动画之前,我想知道是否有更好的(实际工作)方式.这是我目前的代码:
SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position = SCNVector3Make(3,0) cube2.position = SCNVector3Make(-3,0) println("log 0") SCNTransaction.setCompletionBlock { println("log 1") while (animationKey != 1) { //initially animationKey = 0 SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x += 1 cube1.position.y += 1 cube2.position.x += 1 cube2.position.y += 1 println("log 2") SCNTransaction.setCompletionBlock { SCNTransaction.begin() SCNTransaction.setAnimationDuration(2) cube1.position.x -= 1 cube1.position.y -= 1 cube2.position.x -= 1 cube2.position.y -= 1 println("log 3") SCNTransaction.setCompletionBlock { animationKey = 1 } SCNTransaction.commit() } println("log 4") SCNTransaction.commit() } } println("log 5") SCNTransaction.commit() println("log 6") }
输出显示以下内容:
log 0,log 5,log 6,log 1,log 2,log 4,log 2 …然后它继续log 4和log 2交替.
有人可以帮助我吗?
注意
>我认为递归方法会更好,因此我用调用这个方法取代了animationKey = 1 ……根本不再有效.
>我有动画一次工作但现在它在完成第一个动画后完全忽略了第二个和第三个动画块……
解决方法
如果要为SCNNode设置动画,请让节点运行SCNAction.
迅速:
let moveUp = SCNAction.moveBy(x: 0,y: 1,z: 0,duration: 1) moveUp.timingMode = .easeInEaSEOut; let moveDown = SCNAction.moveBy(x: 0,y: -1,duration: 1) moveDown.timingMode = .easeInEaSEOut; let moveSequence = SCNAction.sequence([moveUp,moveDown]) let moveLoop = SCNAction.repeatForever(moveSequence) myNode.runAction(moveLoop)
Objective-C的:
SCNAction *moveUp = [SCNAction moveByX:0 Y:1 Z:0 duration:1]; moveUp.timingMode = SCNActionTimingModeEaseInEaSEOut; SCNAction *moveDown = [SCNAction moveByX:0 Y:-1 Z:0 duration:1]; moveDown.timingMode = SCNActionTimingModeEaseInEaSEOut; SCNAction *moveSequence = [SCNAction sequence:@[moveUp,moveDown]]; SCNAction *moveLoop = [SCNAction repeatActionForever:moveSequence]; [myNode runAction:moveLoop];
上面的代码将永久地上下调整myNode的动画.