Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically put a UISwitch in a SpriteKit/Skcene

Tags:

xcode

ios

swift

I simply am trying to put the default switch that is already built in Xcode in a sKcene. I need to know how to do this in swift or is it possible Thanks.


1 Answers

Yes it is possible. Just use this code in your SKScene class:

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)
}

Helper method:

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

Result:

enter image description here

like image 170
Dharmesh Kheni Avatar answered Jan 28 '26 08:01

Dharmesh Kheni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!