gpt4 book ai didi

ios - 如何利用不同的类切换 SceneKit View

转载 作者:行者123 更新时间:2023-11-29 13:56:35 24 4
gpt4 key购买 nike

我希望用户看到三个 sceneKit View 。这是一个 Xcode Playground,所以我没有 Storyboard,否则这会非常简单。当某个 bool 值变为 true 或 false 时,我需要更改 Playground 的 liveView。

我曾尝试使用 if 语句来检查这两个 bool 值是假还是真,然后根据它们更改我的 liveView,但问题是设置 liveView 的代码只运行一次,这意味着它将始终坚持最先分配的 View 。


physics = false
lit = true
if(lit == true)
{
let vc = LitController()
vc.preferredContentSize = CGSize(width: 375, height: 812) //iPhone X
PlaygroundPage.current.liveView = vc

}
else if(physics == true)
{
let vc = PhysicsController()
vc.preferredContentSize = CGSize(width: 375, height: 812) //iPhone X
PlaygroundPage.current.liveView = vc

} else
{

let vc = MyViewController()
vc.preferredContentSize = CGSize(width: 375, height: 812) //iPhone X
PlaygroundPage.current.liveView = vc
}


我希望它一直运行,以便我可以在必要时切换 View ,这可能吗?如果不是,我还能做些什么来执行我想要的?

最佳答案

我建议将它放在物理变量的 didSet 中。另一种可能的方法是将现有代码包装在一个函数中,并制作另一个更改变量的函数。第二个函数在调用时应该调用第一个函数。这确保只要您显式更改变量,此代码就会运行。但是,我强烈建议为此目的使用 didSet,因为它每次 变量更改时都会运行。如果您感到困惑,这里有几个例子。

class ExampleClass {
var boolean : Bool {
switchViews()
}
func switchViews(){
//Code here
}
}

每当 boolean 值改变时,switchViews() 就会被调用。下一个示例使用两个函数方法。

var boolean:Bool = true
func changeBoolean(_ to:Bool){
boolean = to
switchViews()
}
func switchViews(){
//Code here
}
//Changes boolean value and calls switchViews()
changeBoolean(false)

如前所述,这是另一种方式。我会推荐第一个。希望这会有所帮助,如果上面的代码包含错误或不起作用,请告诉我。

关于ios - 如何利用不同的类切换 SceneKit View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55328621/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com