我正在尝试旋转SCNBox
我创建使用swipe gestures
。例如,当我向右滑动时,该框应旋转 90 度Y-axis
当我向左滑动时为-90degs。为了实现这一点,我一直在使用节点的SCNAction.rotateByX
方法来执行旋转动画。现在我遇到的问题是沿着任一方向旋转时X-axis
or Z-axis
在旋转之后Y-axis
反之亦然,轴的位置发生变化。
我注意到,在 X、Y、Z 轴上执行的任何旋转都会改变其他轴指向的方向。
示例:默认位置
然后在旋转之后Z-axis
:
当然这会带来一个问题,因为现在当我swipe left or right
我不再获得欲望效果,因为X-axis
and Y-axis
现在已经交换了位置。我想知道的是为什么会出现这种情况?是否有办法执行旋转动画而不影响其他轴?
对于我对这个主题缺乏了解,我深表歉意,因为这是我第一次尝试 3D 图形。
解决方案:
func swipeRight(recognizer: UITapGestureRecognizer) {
// rotation animation
let action = SCNAction.rotateByX(0, y: CGFloat(GLKMathDegreesToRadians(90)), z: 0, duration: 0.5)
boxNode.runAction(action)
//repositoning of the x,y,z axes after the rotation has been applied
let currentPivot = boxNode.pivot
let changePivot = SCNMatrix4Invert(boxNode.transform)
boxNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
boxNode.transform = SCNMatrix4Identity
}
我还没有遇到任何问题,但使用完成处理程序可能更安全,以确保在重新定位 X、Y、Z 轴之前完成对 X、Y、Z 轴的任何更改。
我遇到了同样的问题,这是我用来提供所需行为的方法:
func panGesture(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(sender.view!)
let pan_x = Float(translation.x)
let pan_y = Float(-translation.y)
let anglePan = sqrt(pow(pan_x,2)+pow(pan_y,2))*(Float)(M_PI)/180.0
var rotVector = SCNVector4()
rotVector.x = -pan_y
rotVector.y = pan_x
rotVector.z = 0
rotVector.w = anglePan
// apply to your model container node
boxNode.rotation = rotVector
if(sender.state == UIGestureRecognizerState.Ended) {
let currentPivot = boxNode.pivot
let changePivot = SCNMatrix4Invert(boxNode.transform)
boxNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
boxNode.transform = SCNMatrix4Identity
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)