如何以编程方式将UISwitch放入SpriteKit / Skcene中

我只是试图将已经在Xcode中构建的默认开关放在sKcene中。 我需要知道如何在swift中执行此操作,或者是否可能谢谢。

对的,这是可能的。 只需在SKScene类中使用此代码:

 override func didMoveToView(view: SKView) { /* Setup your scene here */ let switchDemo = UISwitch(frame:CGRectMake(150, 300, 0, 0)) switchDemo.on = true switchDemo.setOn(true, animated: false) switchDemo.addTarget(self, action: "switchValueDidChange:", forControlEvents: .ValueChanged) self.view!.addSubview(switchDemo) } 

助手方法:

 func switchValueDidChange(sender:UISwitch!) { if (sender.on == true){ print("on") } else{ print("off") } } 

结果:

在此处输入图像描述

 // Set the desired frame of switch here let onOffSwitch:UISwitch = UISwitch(frame:CGRectMake(2, 2, 30, 30)) //assign an action onOffSwitch.addTarget(self, action: "onOffSwitchTapped:", forControlEvents: UIControlEvents.ValueChanged) //Add in required view self.view.addSubview(onOffSwitch)