在 SCNView 上启用用户交互后,如何使其仅水平旋转



我是SceneKit的新手,我正在尝试做的是将dae文件加载到SCNScene,将此SCNScene设置为SCNView,启用用户交互,然后我可以通过手势旋转3D模型。到目前为止,它进展顺利,当我滑动或放大/缩小时,3D 模型按应有的方式工作。但是,我真正需要的是,当手势(向右或向左滑动)发生时,3D模型仅水平旋转,而没有放大/缩小,我该怎么做才能实现它?这是我的代码:

// 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)以仅水平旋转。

最新更新