Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scaling a SCNNode in runtime using the Pinch gesture

I am trying to scale and SCNNode in real time using the Pinch gesture:

This is my current code

let pinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(from:)))
sceneView.addGestureRecognizer(pinchGestureRecognizer)

@objc
func handlePinch(from recognizer: UIPinchGestureRecognizer){
  var pinchScale = recognizer.scale
  pinchScale = round(pinchScale * 1000) / 1000.0

  sceneView.scene.rootNode.enumerateChildNodes { (node, stop) -> Void in
    if(node.name == "Box01"){
       node.scale = SCNVector3(x: pinchScale, y: pinchScale, z: pinchScale)
    }
  }        
}

However the node doesn't scale big or small? Can someone please point my mistake?

The SCNNode is loaded and has an animation on applied like so,

sceneView.scene.rootNode.addChildNode(node)
loadAnimation(animation: .Attack, sceneName: "art.scnassets/attack", animationIdentifier: "attackID");
like image 229
Pavan K Avatar asked Jun 30 '17 16:06

Pavan K


1 Answers

Got it to work in swift

@objc func handlePinch(gesture: UIPinchGestureRecognizer){
    if(scnnodeSelected){

        if (gesture.state == .changed) {
            let pinchScaleX = Float(gesture.scale) * tappedObjectNode.scale.x
            let pinchScaleY =  Float(gesture.scale) * tappedObjectNode.scale.y
            let pinchScaleZ =  Float(gesture.scale) * tappedObjectNode.scale.z
            tappedObjectNode.scale = SCNVector3(pinchScaleX, pinchScaleY, pinchScaleZ)
            gesture.scale=1
        }
    }
}
like image 64
Roberto Brvé Avatar answered Oct 01 '22 02:10

Roberto Brvé