当我在屏幕上滑动时,我希望SCNNode对象在速度降低时旋转。当速度为0时,动画应该停止。 这是我的代码:
viewDidLoad中:
sceneView.delegate = self
let scene = SCNScene(named: "art.scnassets/Cube.dae")!
self.cubeNode = scene.rootNode.childNode(withName: "Cube", recursively: true)
self.cubeNode?.position = SCNVector3Make(0, 0, -1)
sceneView.scene = scene
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(gesture:)))
swipeRight.direction = UISwipeGestureRecognizerDirection.right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipes(gesture:)))
swipeLeft.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeLeft)
handleSwipes函数:
@objc func handleSwipes(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
animateSpin(direction: .right)
case UISwipeGestureRecognizerDirection.left:
animateSpin(direction: .left)
default:
break
}
}
}
和animateSpin功能:
func animateSpin(direction: SwipeDirection) {
let spin = CABasicAnimation(keyPath: "rotation")
spin.fromValue = NSValue.init(scnVector4: SCNVector4(0, 0, 0, 0))
switch direction {
case .left:
spin.toValue = NSValue.init(scnVector4: SCNVector4(x: 0, y:-1, z:0, w: 2 * .pi))
case .right:
spin.toValue = NSValue.init(scnVector4: SCNVector4(x: 0, y:1, z:0, w: 2 * .pi))
}
spin.speed = 1
spin.repeatCount = 20
self.cubeNode!.addAnimation(spin, forKey: "spin around")
}
我也有一个方向的枚举:
enum SwipeDirection {
case right
case left
}
非常感谢有关如何执行此操作的任何建议。有没有办法在每次动画重复后运行一些代码?
[解决]