[英]SceneKit: pan gesture not storing previous rotation
我正在使用平移手勢旋轉SceneKit場景中的節點。 我當前擁有的代碼完美地旋轉了節點。 這是我用來旋轉節點的代碼:
var previousRotation:Float = 0
@objc func panGestureRecognized(gesture: UIPanGestureRecognizer) {
if gesture.numberOfTouches == 2 {
let view = self.view as! SCNView
let node = view.scene!.rootNode.childNode(withName: "Node", recursively: false)
let translation = gesture.translation(in: view)
var newAngle = Float(translation.x) * Float(Double.pi) / 180.0
newAngle += previousRotation
switch gesture.state {
case .began:
newAngle += previousRotation
break
case .changed:
node!.rotation = SCNVector4(x: 0, y: Float(translation.y), z: 0, w: newAngle)
break
case .ended:
newAngle += previousRotation
break
default: break
}
}
}
問題是,當您抬起手指並再次開始旋轉時,旋轉會“重置”。 我需要它“保持”它的旋轉,所以當您再次開始平移時,它將從上次旋轉停止的位置繼續。
在進行了一些重大更改,其他SO問題的信息以及不同的方法之后,我們找到了答案。 使用此代碼,您可以完美地在任何軸上旋轉節點(在我的情況下為y),然后“存儲”其旋轉以隨后繼續與另一個平移一起旋轉。
var previousRotation = SCNVector4(x: 0, y: 0, z: 0, w: 0)
@objc func pan(gesture: UIPanGestureRecognizer) {
if gesture.numberOfTouches == 2 {
let view = self.view as! SCNView
let node = view.scene!.rootNode.childNode(withName: "Node", recursively: false)
let translate = gesture.translation(in: view)
var newAngle = Float(translate.x) * Float(Double.pi) / 180.0
var rotationVector = SCNVector4()
rotationVector = SCNVector4(x: 0, y: previousRotation.y + Float(translate.y), z: 0, w: previousRotation.w - newAngle)
switch gesture.state {
case .began:
previousRotation = node!.rotation
break
case .changed:
node!.rotation = rotationVector
break
default: break
}
}
}
希望這對以后的人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.