title: Building a 3D cube in iOS with two gestures
body: <p>I can automatically create a 3D cube in iOS using the code below.</p>
  
  <p>The question is: how to build a 3D cube with two gestures? </p>
  
  <p>With <strong>pinch out gesture</strong> I'd like to build a base of cube in XZ axis.</p>
  
  <p>Then with <strong>drag gesture</strong> I'd like to build a height of cube in Y axis.</p>
  
  <p>How to do it?</p>
  
  <p><a href="https://i.stack.imgur.com/kP4xY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kP4xY.png" alt="enter image description here"></a></p>
  
  <pre><code>import UIKit
  import SceneKit
  
  class GameViewController: UIViewController {
      var scnView: SCNView!
      var scnScene: SCNScene!
      var cameraNode: SCNNode!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          setupView()
          setupScene()
          setupCamera()
          spawnShape()
      }
  
      override func shouldAutorotate() -&gt; Bool {
          return true
      }
  
      override func prefersStatusBarHidden() -&gt; Bool {
          return true
      }
  
      func setupView() {
          scnView = self.view as! SCNView
          scnView.allowsCameraControl = true
          scnView.autoenablesDefaultLighting = true
      }
  
      func setupScene() {
          scnScene = SCNScene()
          scnView.scene = scnScene
          scnScene.background.contents = "BG.scnassets/BgTexture.png"
      }
  
      func setupCamera() {
          cameraNode = SCNNode()
          cameraNode.camera = SCNCamera()
          cameraNode.position = SCNVector3(x: 0, y: 0, z: 12)
          scnScene.rootNode.addChildNode(cameraNode)
      }    
  
      func spawnShape() {
          var geometry: SCNGeometry
  
          switch ShapeType.random() {
          case .Box:
          geometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.1)
          }
  
          let geometryNode = SCNNode(geometry: geometry)
          scnScene.rootNode.addChildNode(geometryNode)
      }
  }   
  </code></pre>
  
  <p>I tried to implement <code>UIPinchGestureRecognizer()</code> and <code>UIPanGestureRecognizer()</code> subclasses but I failed. I don't know how to make it.</p>
  
  <pre><code>var pinchGesture = UIPinchGestureRecognizer()
  var dragGesture = UIPanGestureRecognizer()
  
  override func viewDidLoad() {
      super.viewDidLoad()
      setupView()
      setupScene()
      setupCamera()
      spawnShape()
  
      self.scnView.userInteractionEnabled = true
      self.scnView.multipleTouchEnabled = true
  
      self.pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchRecognized(_:)))
      self.dragGesture = UIPanGestureRecognizer(target: self, action: #selector(panRecognized(_:)))
      self.scnView.addGestureRecognizer(self.pinchGesture)
      self.scnView.addGestureRecognizer(self.dragGesture)
  }
  
  @IBAction func pinchRecognized(pinch: UIPinchGestureRecognizer) {
      ........
  }
  @IBAction func panRecognized(pan: UIPanGestureRecognizer) {
      ........
  }
  </code></pre>