我有一个SCNNode,我已经设置在SCNVector3(0,0,-1)的位置。以下代码沿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,但是它没有达到预期的效果。我怎样才能做到这一点?
答案 0 :(得分:3)
尝试这样的事情:
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因子乘以使翻译更顺畅,而且最终用户也更容易。您可以将该因素更改为您喜欢的任何其他因素。