在SCNView上启用用户交互后,如何让它只水平旋转?
After enable user interaction on a SCNView, how to make it only rotate horizontally?
我是 SceneKit 的新手,我想做的是将 dae 文件加载到 SCNScene,将此 SCNScene 设置为 SCNView,启用用户交互,然后我可以旋转 3D 模型手势。到目前为止一切顺利,当我滑动或缩小 in/zoom 时,3D 模型按应有的方式工作。然而,我真正需要的是当一个手势(向右或向左滑动)发生时,3D 模型仅水平旋转,并且没有缩放 in/zoom 缩小,我该怎么做才能实现它?
这是我的代码:
// retrieve the SCNView
SCNView *myView = (SCNView *)self.view;
// load dae file and set the scene to the view
myView.scene = [SCNScene sceneNamed:@"model.dae"];
myView.userInteractionEnabled = YES;
myView.allowsCameraControl = YES;
myView.autoenablesDefaultLighting = YES;
myView.backgroundColor = [UIColor lightGrayColor];
感谢您的帮助!
我不确定您是否可以使用 allowsCameraControl
- 这似乎是与模型交互的非常基本的规定。
如果您向场景添加平移手势,您就可以随心所欲地操纵模型中的任何节点:
- (void)viewDidLoad {
// Add the scene etc....
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[_sceneView addGestureRecognizer:panRecognizer];
}
- (void)panGesture:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:sender.view];
if (sender.state == UIGestureRecognizerStateChanged) {
[self doPanWithPoint:translation];
}
}
- (void)doPanWithPoint:(CGPoint)translation {
CGFloat x = (CGFloat)(translation.y) * (CGFloat)(M_PI)/180.0;
CGFloat y = (CGFloat)(translation.x) * (CGFloat)(M_PI)/180.0;
// Manuipulate the required (root?) node as you see fit
_geometryNode.transform = SCNMatrix4MakeRotation(x, 0, 1, 0);
_geometryNode.transform = SCNMatrix4Mult(_geometryNode.transform, SCNMatrix4MakeRotation(y, 1, 0, 0));
}
显然可以省略第二个旋转步骤(或设置y=0)只水平旋转
我是 SceneKit 的新手,我想做的是将 dae 文件加载到 SCNScene,将此 SCNScene 设置为 SCNView,启用用户交互,然后我可以旋转 3D 模型手势。到目前为止一切顺利,当我滑动或缩小 in/zoom 时,3D 模型按应有的方式工作。然而,我真正需要的是当一个手势(向右或向左滑动)发生时,3D 模型仅水平旋转,并且没有缩放 in/zoom 缩小,我该怎么做才能实现它? 这是我的代码:
// retrieve the SCNView
SCNView *myView = (SCNView *)self.view;
// load dae file and set the scene to the view
myView.scene = [SCNScene sceneNamed:@"model.dae"];
myView.userInteractionEnabled = YES;
myView.allowsCameraControl = YES;
myView.autoenablesDefaultLighting = YES;
myView.backgroundColor = [UIColor lightGrayColor];
感谢您的帮助!
我不确定您是否可以使用 allowsCameraControl
- 这似乎是与模型交互的非常基本的规定。
如果您向场景添加平移手势,您就可以随心所欲地操纵模型中的任何节点:
- (void)viewDidLoad {
// Add the scene etc....
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[_sceneView addGestureRecognizer:panRecognizer];
}
- (void)panGesture:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:sender.view];
if (sender.state == UIGestureRecognizerStateChanged) {
[self doPanWithPoint:translation];
}
}
- (void)doPanWithPoint:(CGPoint)translation {
CGFloat x = (CGFloat)(translation.y) * (CGFloat)(M_PI)/180.0;
CGFloat y = (CGFloat)(translation.x) * (CGFloat)(M_PI)/180.0;
// Manuipulate the required (root?) node as you see fit
_geometryNode.transform = SCNMatrix4MakeRotation(x, 0, 1, 0);
_geometryNode.transform = SCNMatrix4Mult(_geometryNode.transform, SCNMatrix4MakeRotation(y, 1, 0, 0));
}
显然可以省略第二个旋转步骤(或设置y=0)只水平旋转