[英]SceneKit: Using pan gesture to move a node that's not at origin
我有一個已設置在SCNVector3(0,0,-1)位置的SCNNode。 以下代碼沿Z軸向前或向后移動節點,但是,初始平移手勢會將節點移至(0,0,0),然后將節點與平移手勢對齊。
let currentPositionDepth = CGPoint()
@objc func handlePan(sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: sceneView)
var newPos = CGPoint(x: CGFloat(translation.x), y: CGFloat(translation.y))
newPos.x += currentPositionDepth.x
newPos.y += currentPositionDepth.y
node.position.x = Float(newPos.x)
node.position.z = Float(newPos.y)
if(sender.state == .ended) { currentPositionDepth = newPos }
}
我希望節點從它的設置位置(0,0,-1)移動。 我嘗試將currentPositionDepth.y設置為-1,但是無法達到預期的效果。 我該如何實現?
嘗試這樣的事情:
var previousLoc = CGPoint.init(x: 0, y: 0)
@objc func panAirShip(sender: UIPanGestureRecognizer){
var delta = sender.translation(in: self.view)
let loc = sender.location(in: self.view)
if sender.state == .changed {
delta = CGPoint.init(x: 2 * (loc.x - previousLoc.x), y: 2 * (loc.y - previousLoc.y))
airshipNode.position = SCNVector3.init(airshipNode.position.x + Float(delta.x * 0.02), airshipNode.position.y + Float(-delta.y * (0.02)), 0)
previousLoc = loc
}
previousLoc = loc
}
我將0.02系數乘以使翻譯更平滑,從而使最終用戶更輕松。 您可以將該因素更改為您喜歡的其他任何因素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.