Search code examples
swiftxcodeswift-playground

How do I check for a click on my button using SpriteKit?


I created a button, and now I want to check for a click on that button, how do I do this? I am not using any classes in my code. Below is the code I used to create the button.

let startButton = UIButton(type: .system)    
startButton.frame = CGRect(x: 300, y: 400, width: 50, height: 10)
startButton.tintColor = .blue
startButton.setTitle("Next", for:.normal)
view.addSubview(startButton)

I think I need to use the touchesBegan method, but I don't know how to do that properly.

Thank you in advance!


Solution

  • your button

    let startButton = UIButton(type: .system)    
    startButton.frame = CGRect(x: 300, y: 400, width: 50, height: 10)
    startButton.tintColor = .blue
    startButton.setTitle("Next", for:.normal)
    view.addSubview(startButton)
    

    add action

    btn.addTarget(self, action: #selector(TDquizOptionBtn.TapAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    your button action

     func TapAction(sender:UIButton!){
    
          //do whatever you want to do on button action
        }