"Stick" UIView和SCNNode一起做动画
"Stick" UIView and SCNNode together for animation
背景
我想同步移动一个 SCNNode 和一个包含图像的 UIView。 UIView 和 UIImageView 位于节点上,因此看起来它们是 SCNNode(立方体)的纹理。
代码
let move:Float = 15
let moveCube: SCNAction = SCNAction.moveTo(SCNVector3Make(cube.position.x-move, cube.position.y, cube.position.z), duration: 1)
我尝试了什么/我现在是怎么做的
我使用以下方法为 UIView 设置动画:
var move:Float = 15
var projMove = scnView.projectPoint(SCNVector3(x: move, y: 0, z: 0)) //converts 3D coordSystem into 2D
UIView.animateWithDuration(1, delay: 0, options: nil | UIViewAnimationOptions.CurveEaseOut, animations: {
self.myView.center.x = CGFloat(-projMove.x)
}, completion: { finished in
})
这有效,立方体向左移动,UIView 也是如此。
但是这段代码并不是我认为最好的解决方案。
问题
- 有没有更好的方法让立方体向左移动,包括UIView?
- 同样,我想同时移动它,最好使用一个代码段
- 我可以将一个表面(例如正面)的纹理设置为图像吗?
- 我想将图像设置为只有一侧的纹理
- 我可以将整体纹理设置为图像,然后使用它的 Alpha 通道将图像放在它上面吗?
- 加起来 #2,我想将立方体的纹理设置为一种颜色,高于我想投影图像的颜色(它有 alpha 层所以颜色应该仍然可见。)
提前致谢:)
Is there a better way to let the cube move left, including the UIView?
是的。您可以取消投影视图的坐标并将其用作移动立方体的参考。参见 Unproject Point
Can I possibly set one surface's (the front e.g.) texture to the image instead?
是的,只需将立方体 material 的漫反射通道设置为您的 UIImage。
Could I even set the overall texture to an image and then put the image above it using it's alpha channel?
也许,我不太确定你在说什么,你介意对此进行扩展吗?如果我了解一点 Spritekit 将是你最好的选择。
以下是评论的更新答案:
I do use the unprojected points before projecting them in the SCNAction. And I meant more like moving both at once instead of a separate animation for each.
我认为没有。您可以为第三方 属性 设置动画并更改其 setter 以同时更改视图和节点。您也可以使用块,但最后您不能 link 直接将两者
Well, I want to set the image only to one side of the cube.
您可以简单地提供一个包含 6 个 material 的数组,其中一个是您的图像,另外 5 个是每秒 material 进行填充。您需要使用顺序来查找图像需要在数组中的位置。
That relates to #2, I want to set the texture to a color and then set one side's texture to the image I want to use.
有两种方法。您可以使用将图像添加到纯色之上的着色器,或者您可以制作第二个稍小(小于 1%)的立方体,并使该立方体成为您想要的背景颜色。然后,在较大的图像上使用透明图像。
背景
我想同步移动一个 SCNNode 和一个包含图像的 UIView。 UIView 和 UIImageView 位于节点上,因此看起来它们是 SCNNode(立方体)的纹理。
代码
let move:Float = 15
let moveCube: SCNAction = SCNAction.moveTo(SCNVector3Make(cube.position.x-move, cube.position.y, cube.position.z), duration: 1)
我尝试了什么/我现在是怎么做的
我使用以下方法为 UIView 设置动画:
var move:Float = 15
var projMove = scnView.projectPoint(SCNVector3(x: move, y: 0, z: 0)) //converts 3D coordSystem into 2D
UIView.animateWithDuration(1, delay: 0, options: nil | UIViewAnimationOptions.CurveEaseOut, animations: {
self.myView.center.x = CGFloat(-projMove.x)
}, completion: { finished in
})
这有效,立方体向左移动,UIView 也是如此。
但是这段代码并不是我认为最好的解决方案。
问题
- 有没有更好的方法让立方体向左移动,包括UIView?
- 同样,我想同时移动它,最好使用一个代码段
- 同样,我想同时移动它,最好使用一个代码段
- 我可以将一个表面(例如正面)的纹理设置为图像吗?
- 我想将图像设置为只有一侧的纹理
- 我可以将整体纹理设置为图像,然后使用它的 Alpha 通道将图像放在它上面吗?
- 加起来 #2,我想将立方体的纹理设置为一种颜色,高于我想投影图像的颜色(它有 alpha 层所以颜色应该仍然可见。)
提前致谢:)
Is there a better way to let the cube move left, including the UIView?
是的。您可以取消投影视图的坐标并将其用作移动立方体的参考。参见 Unproject Point
Can I possibly set one surface's (the front e.g.) texture to the image instead?
是的,只需将立方体 material 的漫反射通道设置为您的 UIImage。
Could I even set the overall texture to an image and then put the image above it using it's alpha channel?
也许,我不太确定你在说什么,你介意对此进行扩展吗?如果我了解一点 Spritekit 将是你最好的选择。
以下是评论的更新答案:
I do use the unprojected points before projecting them in the SCNAction. And I meant more like moving both at once instead of a separate animation for each.
我认为没有。您可以为第三方 属性 设置动画并更改其 setter 以同时更改视图和节点。您也可以使用块,但最后您不能 link 直接将两者
Well, I want to set the image only to one side of the cube.
您可以简单地提供一个包含 6 个 material 的数组,其中一个是您的图像,另外 5 个是每秒 material 进行填充。您需要使用顺序来查找图像需要在数组中的位置。
That relates to #2, I want to set the texture to a color and then set one side's texture to the image I want to use.
有两种方法。您可以使用将图像添加到纯色之上的着色器,或者您可以制作第二个稍小(小于 1%)的立方体,并使该立方体成为您想要的背景颜色。然后,在较大的图像上使用透明图像。