[英]Animate SCNNode forever - SceneKit, Swift
I want to animate 2 cubes forever, until the user taps one of it. 我想永远动画2个立方体,直到用户点击其中一个。 The animation is supposed to be like an ∞.
动画应该像∞一样。
These are my cubes in the sceneView I want to animate: 这些是我想要动画的sceneView中的我的立方体:
I am trying to do that with nested animations and it does partially work. 我试图用嵌套动画做到这一点,它确实部分工作。 Now, since the animations always wait until they're finished before starting the new one, it does not look smooth at all.
现在,由于动画总是等到它们在开始新动画之前完成,它看起来并不光滑。
Before continuing to develop the ∞-animation I would like to know if there is a better (and actually working) way. 在继续开发∞动画之前,我想知道是否有更好的(实际工作)方式。 This is my current code:
这是我目前的代码:
SCNTransaction.begin()
SCNTransaction.setAnimationDuration(2)
cube1.position = SCNVector3Make(3, 0, 0)
cube2.position = SCNVector3Make(-3, 0, 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")
}
The output shows the following things: log 0, log 5, log 6, log 1, log 2, log 4, log 2
...and then it just continues with log 4
and log 2
alternating. 输出显示以下内容:
log 0, log 5, log 6, log 1, log 2, log 4, log 2
...然后它继续log 4
和log 2
交替。
Can someone help me there? 有人可以帮助我吗?
Note 注意
I thought a recursive approach would be better, I therefore replaced animationKey = 1
with calling the method this is in...didn't work at all anymore. 我认为递归方法会更好,因此我用调用此方法替换了
animationKey = 1
......根本不再有效。
I got the animations to work once but now it just stops after finishing the very first animation and totally ignores the 2nd and 3rd animation block... 我让动画工作一次,但现在它只是在完成第一个动画后停止并完全忽略了第二和第三个动画块......
If you're going to animate a SCNNode
have your nodes run a SCNAction
. 如果要为
SCNNode
设置动画,请让节点运行SCNAction
。
Swift: 迅速:
let moveUp = SCNAction.moveBy(x: 0, y: 1, z: 0, duration: 1)
moveUp.timingMode = .easeInEaseOut;
let moveDown = SCNAction.moveBy(x: 0, y: -1, z: 0, duration: 1)
moveDown.timingMode = .easeInEaseOut;
let moveSequence = SCNAction.sequence([moveUp,moveDown])
let moveLoop = SCNAction.repeatForever(moveSequence)
myNode.runAction(moveLoop)
Objective-C: 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];
The code above will animate myNode
up and down forever. 上面的代码将
myNode
地上下myNode
动画。
you can create a CAKeyframeAnimation
animation from a Bezier path (see the path
property). 您可以从Bezier路径创建
CAKeyframeAnimation
动画(请参阅path
属性)。 That way you will get a very smooth animation. 这样你就可以得到一个非常流畅的动画。
For it to repeat forever set its repeatCount
to HUGE_VALF
, and you can adjust the velocity using the timingFunction
property. 为了永远重复它将
repeatCount
设置为HUGE_VALF
,您可以使用timingFunction
属性调整速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.