【发布时间】:2019-04-14 18:46:15
【问题描述】:
我试图让一个 Sphere 节点跟随我的手指在屏幕上的移动仅在它的 x 轴上,而不影响球体的 y 或 z 值。我不确定是否应该对物理体施加力或使用 SCNAction 移动它。球体将在占据屏幕宽度的立方体节点上弹跳。到目前为止,我最大的问题是弄清楚要在 pangesture 函数中放入什么。这是我尝试复制的示例,用户在屏幕上滑动手指,球会反映其位置。
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches {
let touchLocation = t.location(in: self.view)
let touchCoordinatesX = (touchLocation.x - self.scnView.frame.width / 2)
ballNode.position.x = Float(touchCoordinatesX/75)
}
}
上面的代码有点作用,当我在屏幕上拖动手指时,y 位置会出现异常,并且会非常迅速地上下滞后,即使我的代码从未编辑过球的 y 值。每次我在屏幕上拖动时,当球被添加到场景的根节点时,球的 y 位置都会重置为其原始值。
根据您的回答,这就是我所使用的,并且完全符合我的需要。
@objc func handlePan(recognizer: UIPanGestureRecognizer)
{
currentLocation = recognizer.location(in: self.view)
let touchCoordinatesX = (currentLocation.x - self.scnView.frame.width / 2)
if recognizer.state == .changed
{
ballNode.position.x = Float(touchCoordinatesX/75)
ballNode.position.y = ballNode.presentation.position.y
}
}
【问题讨论】:
标签: ios swift4 uigesturerecognizer scenekit uipangesturerecognizer