1

我想选择一个节点(下面的代码),然后将它拖到“表”上。选择代码很简单,但我不确定如何拖动选定的节点。我的尝试如下。

其中一些代码来自带有 SceneKit 的 3D 图形

    - (void)handleTap:(UIGestureRecognizer*)gestureRecognize
{
    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;

    // check what nodes are tapped
    CGPoint p = [gestureRecognize locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];

    // check that we clicked on at least one object
    if([hitResults count] > 0){
        // retrieved the first clicked object
        SCNHitTestResult *result = [hitResults objectAtIndex:0];

        // get its material
        SCNMaterial *material = result.node.geometry.firstMaterial;

        [SCNTransaction begin];
        [SCNTransaction setAnimationDuration:0.5];

        // on completion - unhighlight
        [SCNTransaction setCompletionBlock:^{
            [SCNTransaction begin];
            [SCNTransaction setAnimationDuration:0.5];

            isSelected = YES;

            [SCNTransaction commit];
        }];

        material.emission.contents = [UIColor redColor];

        [SCNTransaction commit];
    }
}

现在我尝试拖动选定的节点:

- (void)handlePan:(UIPanGestureRecognizer *)gesture {

CGPoint point = [gesture velocityInView:self.view];
CGPoint p = [gesture locationInView:scene.sceneView];

// nothing selected, so spin the table node
if ( !isSelected) {
    if (point.x > 0)
    {
        // user dragged towards the right
        [nodeForRotation runAction:[SCNAction repeatAction:[SCNAction rotateByX:0 y:point.x/10000 z:0 duration:0.23] count:1]];
    } else {
        // user dragged towards the left
        [nodeForRotation runAction:[SCNAction repeatAction:[SCNAction rotateByX:0 y:point.x/10000 z:0 duration:0.23] count:1]];
    }
} else {

// node on table selected, so move this node

    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;

    // check what nodes are tapped
    CGPoint p = [gesture locationInView:scnView];
    NSArray *hitResults = [scnView hitTest:p options:nil];
    if([hitResults count] > 0){
        // retrieved the first clicked object
        SCNHitTestResult *result = [hitResults objectAtIndex:0];
        result.node.position = SCNVector3Make(p.x,p.y,0);
    }

}

4

0 回答 0