如何使用UIPanGesture拖动SCNNode

时间:2015-11-16 23:11:29

标签: objective-c drag scenekit uipangesturerecognizer scnnode

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

部分代码来自带有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);
    }

}

0 个答案:

没有答案