I created a 3d cube using SceneKit and added a gesture to recognize swiping right. But, I do not know how to rotate the cube to another face when swiped right. I do not want the cube to rotate continuously when swiped, only moving to another face that is on the right side. Sorry for any confusions.
Here is my code where I created cube:
import UIKit
import SceneKit
class ViewController: UIViewController {
// UI
@IBOutlet weak var geometryLabel: UILabel!
@IBOutlet weak var sceneView: SCNView!
// Geometry
var geometryNode: SCNNode = SCNNode()
// Gestures
var currentAngle: Float = 0.0
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// MARK: Scene
sceneSetup()
}
func sceneSetup() {
let scene = SCNScene()
sceneView.scene = scene
sceneView.allowsCameraControl = false
sceneView.autoenablesDefaultLighting = true
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(0, 0, 25)
scene.rootNode.addChildNode(cameraNode)
var geometries = [
SCNBox(width: 8.0, height: 8.0, length: 8.0, chamferRadius: 1.0)
]
var materials = [SCNMaterial]()
for i in 1...6 {
let material = SCNMaterial()
if i == 1 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
if i == 2 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
if i == 3 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
if i == 4 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
if i == 5 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
if i == 6 { material.diffuse.contents = UIColor(red:0.02, green:0.98, blue:0.98, alpha:1.0) }
materials.append(material)
}
for i in 0..<geometries.count {
let geometry = geometries[i]
let node = SCNNode(geometry: geometry)
node.geometry?.materials = materials
scene.rootNode.addChildNode(node)
}
let swipeRight: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
swipeRight.direction = .right
sceneView.addGestureRecognizer(swipeRight)
}
Function for swipe right gesture when recognize:
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped Right")
default:
break
}
}
}
You need to use a
SCNAction.rotate
getting the current w value of the node rotation and adding the amount of angle you want, using this 2 extensions for converting to Radians and from Radians, replace my self.shipNode by your cube node, and change the axis if you need to, taking in account that the first 0 in theSCNVector4Make
sentence is X axisThis is an example
To rotate down you only have to change the axis of the rotation you can use this
UPDATED Using your code You need to use
SCNAction.rotate(by: #Float, around: #SCNVector3, duration: #duration)
method instead, below is the full code for your requeriments