我只是试图将已经在Xcode中构建的默认开关放在sKcene中。我需要知道如何在swift中执行此操作,或者是否可以谢谢。
答案 0 :(得分:6)
是的,这是可能的。只需在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")
}
}
结果:
答案 1 :(得分:0)
// 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)
答案 2 :(得分:0)
已更新为快速5
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let switchDemo = UISwitch(frame:CGRect(x: 150, y: 300, width: 0, height: 0))
switchDemo.isOn = true
switchDemo.setOn(true, animated: false)
switchDemo.addTarget(self, action: #selector(switchValueDidChange(_:)), for: .valueChanged)
self.view!.addSubview(switchDemo)
}
Helper方法:
@objc func switchValueDidChange(_ sender: UISwitch!) {
if (sender.isOn == true){
print("on")
}
else{
print("off")
}
}