2015-08-13 5 views
5

Mit dem unten gezeigten Code kann ich eine Kugel in meiner Szene.mit einem blauen Hintergrund ... Ich bin neu in SceneKit in iOS.Ich möchte wissen, wie wir dieses Objekt bewegen können (Ziehen Sie die Kugel) an eine andere Position (Drop).Scenekit - wie kann ich Drag & Drop

override func viewDidLoad() { 
    super.viewDidLoad() 
    sceneView = SCNView(frame: self.view.frame) 
    sceneView.scene = SCNScene() 
    self.view.addSubview(sceneView) 


    let groundGeometry = SCNFloor() 
    groundGeometry.reflectivity = 0 
    let groundMaterial = SCNMaterial() 
    groundMaterial.diffuse.contents = UIColor.blueColor() 
    groundGeometry.materials = [groundMaterial] 
    ground = SCNNode(geometry: groundGeometry) 

    let camera = SCNCamera() 
    camera.zFar = 10000 
    self.camera = SCNNode() 
    self.camera.camera = camera 
    self.camera.position = SCNVector3(x: -20, y: 15, z: 20) 
    let constraint = SCNLookAtConstraint(target: ground) 
    constraint.gimbalLockEnabled = true 
    self.camera.constraints = [constraint] 

    let ambientLight = SCNLight() 
    ambientLight.color = UIColor.darkGrayColor() 
    ambientLight.type = SCNLightTypeAmbient 
    self.camera.light = ambientLight 

    let spotLight = SCNLight() 
    spotLight.type = SCNLightTypeSpot 
    spotLight.castsShadow = true 
    spotLight.spotInnerAngle = 70.0 
    spotLight.spotOuterAngle = 90.0 
    spotLight.zFar = 500 
    light = SCNNode() 
    light.light = spotLight 
    light.position = SCNVector3(x: 0, y: 25, z: 25) 
    light.constraints = [constraint] 

    let sphereGeometry = SCNSphere(radius: 1.5) 
    let sphereMaterial = SCNMaterial() 
    sphereMaterial.diffuse.contents = UIColor.greenColor() 
    sphereGeometry.materials = [sphereMaterial] 
    sphere1 = SCNNode(geometry: sphereGeometry) 
    sphere1.position = SCNVector3(x: -15, y: 1.5, z: 0) 


    sceneView.scene?.rootNode.addChildNode(self.camera) 
    sceneView.scene?.rootNode.addChildNode(ground) 
    sceneView.scene?.rootNode.addChildNode(light) 

    sceneView.scene?.rootNode.addChildNode(sphere1) 

} 

Antwort

1

Mee, versuchte ich sceneKit mit Objective-c paar Monaten sein kann, es können Sie die Lösung helfen verallgemeinern. Zum Ziehen und Ablegen von Knoten habe ich UIPanGestureRecognizer verwendet.

// Gestenerkenner zum Schwenken (Verschieben)

UIGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)]; 
[[self view] addGestureRecognizer:panGestureRecognizer]; 

// Dann in "handlePanFrom"

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer { 


    if (recognizer.state == UIGestureRecognizerStateChanged) 
    { 
     //Getting the new location of the view after pan gesture 
     CGPoint translation = [recognizer translationInView:recognizer.view]; 

     //Converting this translation as per the SpriteKit coordinates 
     translation = CGPointMake(translation.x, -translation.y); 

     //Changing the position of sphere node with the translation 
     CGPoint position = [sphere position]; 
     [sphere setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)]; 

     //Resetting the values of recognizer 
     [recognizer setTranslation:CGPointZero inView:recognizer.view]; 

    } 
    else if (recognizer.state == UIGestureRecognizerStateEnded) 
    { 
     float scrollDuration = 1; 
     CGPoint velocity = [recognizer velocityInView:recognizer.view]; 

     //Taking current position of sphere node 
     CGPoint pos = [sphere position]; 

     //Finding change in position on the basis of velocity and scroll duration 
     CGPoint p = mult(velocity, scrollDuration); 

     //Calculating new position of the sphere 
     CGPoint newPos = CGPointMake(pos.x + p.x, pos.y - p.y); 

     [sphere removeAllActions]; 

     //Action for moving the sprite to the new location 
     SKAction *moveTo = [SKAction moveTo:newPos duration:scrollDuration]; 
     [moveTo setTimingMode:SKActionTimingEaseOut]; 
     [sphere runAction:moveTo]; 
    } 
} 

Weitere Hilfe finden Sie dieses schöne Tutorial http://www.raywenderlich.com/44270/sprite-kit-tutorial-how-to-drag-and-drop-sprites

+0

Hai überprüfen .. Danke für den Code..Aber es funktioniert nicht gut ... Sphere ist SCNNode und ich kann SKAction nicht geben (Ich importierte SpriteKit und versuchte SKAction) und auch Problem mit der Position: Wie ich bekomme Position in SCNVector 3 ..conversion es zu CGPoint fehlgeschlagen .. – Mee

+0

Mee .. sorry für die Bereitstellung von Code für SpriteKit anstelle von SceneKit. Ich hätte darauf achten müssen. – Jaideep

+0

können Sie den Code für Scenekit aktualisieren – madLokesh