我一直在尝试使用UIPanGestureRecognizer移动附加到iOS ARKit中的UISceneView的SCNNode。它在x方向上可以很好地移动,但是我似乎无法弄清楚如何在y方向上移动它。我采用了两种不同的方法,每种方法都没有成功:
对于为什么y维度似乎与x行为不同的任何见解,将不胜感激。这可能与仅在XZ平面上的运动有关吗?这是上面#2中引用的GitHub示例代码之后的代码:
=============== >>
//移动SCNNode-(void)handlePanGesture:(UIPanGestureRecognizer *)pagr{
switch (pagr.state) { case UIGestureRecognizerStateBegan: { CGPoint tapPoint = [pagr locationInView:sceneView]; NSLog(@"%s tapPoint %@",__FUNCTION__,NSStringFromCGPoint(tapPoint)); NSArray *hitResults = [sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint | ARHitTestResultTypeEstimatedHorizontalPlane]; lastHitTestResult = [hitResults firstObject]; } break; case UIGestureRecognizerStateChanged: { CGPoint tapPoint = [pagr locationInView:sceneView]; if (windScreenView.buttonState == GypsyTargetAdded) { NSArray *hitResults = [sceneView hitTest:tapPoint types:ARHitTestResultTypeFeaturePoint | ARHitTestResultTypeEstimatedHorizontalPlane]; ARHitTestResult *result = [hitResults lastObject]; [SCNTransaction begin]; SCNMatrix4 lastMatrix = SCNMatrix4FromMat4(lastHitTestResult.worldTransform); SCNVector3 lastVector = SCNVector3Make(lastMatrix.m41, lastMatrix.m42, lastMatrix.m43); SCNMatrix4 newMatrix = SCNMatrix4FromMat4(result.worldTransform); SCNVector3 newVector = SCNVector3Make(newMatrix.m41, newMatrix.m42, newMatrix.m43); CGFloat dx = newVector.x-lastVector.x; CGFloat dy = newVector.y-lastVector.y; SCNVector3 adjVector = SCNVector3Make(gypsyTargetNode.position.x + dx, gypsyTargetNode.position.y + dy, gypsyTargetNode.position.z); gypsyTargetNode.position = adjVector; [SCNTransaction commit]; NSLog(@"%s lastVector: x = %f, y = %f, z = %f",__FUNCTION__,lastVector.x,lastVector.y,lastVector.z); NSLog(@"%s newVector: x = %f, y = %f, z = %f",__FUNCTION__,newVector.x,newVector.y,newVector.z); NSLog(@"%s dx = %f, dy = %f",__FUNCTION__,dx,dy); NSLog(@"%s gypsyTargetNode.position: x = %f, y = %f, z = %f",__FUNCTION__,gypsyTargetNode.position.x,gypsyTargetNode.position.y,gypsyTargetNode.position.z); NSLog(@"%s hitResults.count: %li",__FUNCTION__,hitResults.count); lastHitTestResult = result; } } break; case UIGestureRecognizerStateEnded: { lastHitTestResult = nil; } break; default: break; }
}
这是输出控制台的片段。请注意,计算出的“ dx”始终为.000000。
[CalibrationController handlePanGesture:] tapPoint {207.33332824707031, 507}
[CalibrationController handlePanGesture:] lastVector: x = -0.016018, y = -0.083625, z = 0.006696
[CalibrationController handlePanGesture:] newVector: x = -0.015562, y = -0.083862, z = 0.006658
[CalibrationController handlePanGesture:] dx = 0.000456, dy = **-0.000237**
[CalibrationController handlePanGesture:] gypsyTargetNode.position: x = -0.018142, y = -0.087894, z = 0.008041
[CalibrationController handlePanGesture:] hitResults.count: 2
[CalibrationController handlePanGesture:] lastVector: x = -0.015562, y = -0.083862, z = 0.006658
[CalibrationController handlePanGesture:] newVector: x = -0.015562, y = -0.083862, z = 0.006658
[CalibrationController handlePanGesture:] dx = 0.000000, dy = **0.000000**
[CalibrationController handlePanGesture:] gypsyTargetNode.position: x = -0.018142, y = -0.087894, z = 0.008041
[CalibrationController handlePanGesture:] hitResults.count: 2
[CalibrationController handlePanGesture:] lastVector: x = -0.015562, y = -0.083862, z = 0.006658
[CalibrationController handlePanGesture:] newVector: x = -0.015150, y = -0.083862, z = 0.006565
[CalibrationController handlePanGesture:] dx = 0.000412, dy = **0.000000**
[CalibrationController handlePanGesture:] gypsyTargetNode.position: x = -0.017730, y = -0.087894, z = 0.008041
[CalibrationController handlePanGesture:] hitResults.count: 2
[CalibrationController handlePanGesture:] lastVector: x = -0.015150, y = -0.083862, z = 0.006565
[CalibrationController handlePanGesture:] newVector: x = -0.014686, y = -0.083862, z = 0.006361
[CalibrationController handlePanGesture:] dx = 0.000463, dy = **0.000000**
我一直在尝试使用UIPanGestureRecognizer移动附加到iOS ARKit中的UISceneView的SCNNode。它在x方向上可以很好地移动,但是我似乎无法弄清楚如何在y上移动它。
对于平移手势,请执行以下方法: