How to trigger a notification if label is dropped on wrong target?

59 Views Asked by At

In this drag and drop game (adapted from this tutorial), the player has to match the label to the right target. I have it set so that if the touch ends and the label's center is inside the target, it is removed from the screen and the game starts over.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

  if label.name == "letters" {
    if lettersBin.frame.contains(label.position) {
        // remove it and create a new label
        label.removeFromParent()
        setupDragLabel()

However, I want to trigger a notification if the player drops the wrong label onto the bin, so:

else if label.name == "numbers" {
 if lettersBin.contains(label.position) ...

What would I write to finish this? Do I make another label appear on the screen?

2

There are 2 best solutions below

3
binaryPilot84 On BEST ANSWER

It might be easiest to use a UIAlertController. You can add the messages along with button actions to display that the user was wrong.

let converterAction = UIAlertController(title: "Your title here", message: "Your message to the user here", preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default){ _ in
    //Any custom action to happen here
}
converterAction.addAction(okAction)

present(converterAction, animated: true, completion: nil)
0
Carsten Hagemann On

If you do not want to show an alert, you can do the following:

  1. Add a label to the Storyboard and to your file.
  2. Set that label to be hidden by default.
  3. Programatically set label.isHidden = false and set it's text as you wish.

What I now labeled "label" can be anything though, you could even create a group of views and hide/show them all at once. Useful when wanting to show a Progress Indicator and a "Loading..." Label at the same time.