向右滑动时如何旋转到3d立方体的另一面
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向右滑动时如何旋转到3d立方体的另一面相关的知识,希望对你有一定的参考价值。
我使用SceneKit创建了一个3d立方体,并添加了一个手势来识别向右滑动。但是,当我向右滑动时,我不知道如何将立方体旋转到另一个面。我不希望立方体在滑动时连续旋转,只是移动到右侧的另一个面。对不起任何混淆。
这是我创建立方体的代码:
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)
}
识别时滑动右手势的功能:
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped Right")
default:
break
}
}
}
答案
你需要使用SCNAction.rotate
获取节点旋转的当前w值并添加你想要的角度,使用这2个扩展来转换为Radians和Radians,用你的立方体节点替换我的self.shipNode,并改变轴如果你需要,考虑到SCNVector4Make
句子中的前0是X轴
这是一个例子
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
var radiansToDegrees: Double { return Double(self) * 180 / .pi }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
@objc func handleTap(_ gestureRecognize: UIGestureRecognizer) {
//here we rotate the ship node 30 grades in the Y axis each time
self.shipNode?.runAction(SCNAction.rotate(toAxisAngle: SCNVector4Make(0, 1, 0, (self.shipNode?.rotation.w)! + Float(30.degreesToRadians)), duration: 1))
}
要向下旋转,您只需要更改可以使用它的旋转轴
- (void) handleTap:(UIGestureRecognizer*)gestureRecognize
{
[self.ship runAction:[SCNAction rotateToAxisAngle:SCNVector4Make(1, 0, 0, [self getRadiansFromAngle:[self getAngleFromRadian:self.ship.rotation.w] + 30]) duration:1]];
}
更新使用您的代码您需要使用SCNAction.rotate(by: #Float, around: #SCNVector3, duration: #duration)
方法,下面是您的requeriments的完整代码
@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
print("Swiped Right")
geometryNode.runAction(SCNAction.rotate(by: CGFloat(90.degreesToRadians), around: SCNVector3Make(0, 1, 0), duration: 0.2))
case UISwipeGestureRecognizerDirection.left:
print("Swiped Left")
geometryNode.runAction(SCNAction.rotate(by: CGFloat(-90.degreesToRadians), around: SCNVector3Make(0, 1, 0), duration: 0.2))
case UISwipeGestureRecognizerDirection.down:
print("Swiped Down")
geometryNode.runAction(SCNAction.rotate(by: CGFloat(90.degreesToRadians), around: SCNVector3Make(1, 0, 0), duration: 0.2))
default:
break
}
}
}
以上是关于向右滑动时如何旋转到3d立方体的另一面的主要内容,如果未能解决你的问题,请参考以下文章