【问题标题】:After enable user interaction on a SCNView, how to make it only rotate horizontally?在 SCNView 上启用用户交互后,如何使其仅水平旋转?
【发布时间】:2017-06-19 03:31:09
【问题描述】:
我是 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];
感谢您的帮助!
【问题讨论】:
标签:
ios
objective-c
xcode
scenekit
sceneview
【解决方案1】:
我不确定您是否可以使用 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)只水平旋转。