【问题标题】:How to animate the background color of a SCNView (not a SCNScene or UIView)如何为 SCNView(不是 SCNScene 或 UIView)的背景颜色设置动画
【发布时间】:2022-12-13 07:27:57
【问题描述】:

是否可以为 SCNView.backgroundColor 属性设置动画?

请注意,为实际场景 (SCNScene) 制作背景动画很容易,我知道该怎么做。为传统的UIView设置动画背景也很容易。

我一直无法弄清楚如何为 SCNView.backgroundColor 属性设置动画。

【问题讨论】:

    标签: ios swift scenekit scnview


    【解决方案1】:

    假设你使用默认的 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 我运气不好。希望我能以某种方式提供帮助。

    【讨论】:

    【解决方案2】:

    只是对@ZAY 惊人而正确的答案的补充。

    你必须做一个逐帧的颜色动画,

    由于某些原因,

    1. 你在场景视图

    2. 但。您在场景根节点.

      所以,这是一个奇迹。

      完美运行。

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多