是否可以将SCNView
的.backgroundColor
属性动画化?
请注意,这是很容易动画背景的实际场景(SCNScene
),我知道如何做到这一点。这也很容易动画的背景,一个传统的UIView
。
我还没能弄清楚如何动画SCNView
的.backgroundColor
属性。
假设你采用默认的SceneKit游戏模板(带有旋转Jet的那个),我通过这样做得到它的工作:
这是我的viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
let scene = SCNScene() // SCNScene(named: "art.scnassets/ship.scn")!
// create and add a camera to the scene
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
// create and add a light to the scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light!.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
scene.rootNode.addChildNode(lightNode)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light!.type = .ambient
ambientLightNode.light!.color = UIColor.darkGray
scene.rootNode.addChildNode(ambientLightNode)
// retrieve the ship node
// let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!
// animate the 3d object
// ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
// retrieve the SCNView
let scnView = self.view as! SCNView
// set the scene to the view
scnView.scene = scene
// allows the user to manipulate the camera
scnView.allowsCameraControl = true
// show statistics such as fps and timing information
scnView.showsStatistics = true
// Configure the initial background color of the SCNView
scnView.backgroundColor = UIColor.red
// Setup a SCNAction that rotates i.Ex the HUE Value of the Background
let animColor = SCNAction.customAction(duration: 10.0) { _ , timeElapsed in
scnView.backgroundColor = UIColor.init(hue: timeElapsed/10, saturation: 1.0, brightness: 1.0, alpha: 1.0)
}
// Run the Action (here using the rootNode)
scene.rootNode.runAction(animColor)
// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
scnView.addGestureRecognizer(tapGesture)
}
这可能不是最好的解决方案,但是使用SCNTransaction我没有运气。希望我能帮上忙。
只是对令人惊叹的&@ZAY的正确答案
你需要逐帧制作颜色动画,
由于某种原因,
-
在场景视图上执行动作
-
。在场景的根节点上运行操作.
所以,这是一个奇迹。
完美工作。