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");
Update for Swift 5 :
and my
didMove
function :I used this to handle the scale:
Got it to work in swift
I've found that this works pretty well. I also use both touches to try and detect if I'm pinching on an object or not, so you can grab the SCNNode with either finger.
Note, the line that resets the scale is necessary, otherwise it acts really buggy. I can't particularly say why that it is the case though.
Hope this helps